Print row on 4th column to all row

Dear All,

I have input :

SEG901 5173 9005    5740
SEG902 5227 5284    
SEG903 5284 5346    
SEG904 5346 9010    
SEG905 5400 5456    
SEG906 5456 5511    
SEG907 5511 9011    
SEG908 5572 9015    
SEG909 5622 9020    
SEG910 5678 5739    
SEG911 5739 5796    
SEG912 5796 9025    
SEG913 5845 5910    
SEG914 5910 9031    
SEG915 5965 9031    
SEG916 6027 6082    
SEG917 6082 9036    
SEG918 6129 6182    
SEG919 6182 6235    
SEG920 6235 9041    
SEG921 6290 6357    
SEG922 6357 9046    
SEG923 6406 6466    
SEG924 6466 6521    
SEG925 6521 9051    
SEG926 6572 6637    
SEG927 6637 9056    
SEG928 6697 6757    
SEG929 6757 6812    

We need to print row on 4th column to all row.

Expected result:

SEG901 5173 9005 5740
SEG902 5227 5284 5740    
SEG903 5284 5346 5740   
SEG904 5346 9010 5740    
SEG905 5400 5456 5740    
SEG906 5456 5511 5740    
SEG907 5511 9011 5740    
SEG908 5572 9015 5740    
SEG909 5622 9020 5740    
SEG910 5678 5739 5740    
SEG911 5739 5796 5740    
SEG912 5796 9025 5740    
SEG913 5845 5910 5740    
SEG914 5910 9031 5740    
SEG915 5965 9031 5740    
SEG916 6027 6082 5740    
SEG917 6082 9036 5740    
SEG918 6129 6182 5740    
SEG919 6182 6235 5740    
SEG920 6235 9041 5740    
SEG921 6290 6357 5740    
SEG922 6357 9046 5740    
SEG923 6406 6466 5740    
SEG924 6466 6521 5740    
SEG925 6521 9051 5740    
SEG926 6572 6637 5740    
SEG927 6637 9056 5740    
SEG928 6697 6757 5740    
SEG929 6757 6812 5740    

Thanks for advance.
Attila

awk 'NR == 1{p=$4; print $0; next} {$4 = ($4 == "") ? p : $4}1' file
1 Like

This is a little bit simpler than SriniShoo's script and seems to do what was requested (but it doesn't print the trailing spaces that were included in the expected results shown in the 1st message in this thread):

awk '
NR == 1 { r1f4 = $4 }
	{ $4 = r1f4 }
1' input

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or /usr/xpg6/bin/awk .

2 Likes

SriniShoo, it work.

Thank you.