Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

replace column with another column data sed/awk 2

Status
Not open for further replies.

kaanlu

Programmer
Jan 13, 2005
9
US
Hi,

I have a batch file in which the first line is the batch header info and the rest is dataline records. I want to get the 14th and 15th fields from the first line and add those two digits after 24th field of every dataline in the batch.

$$$BRS133010305BRS ACCT FEED * NM05
0610401001410 0103BRS
0610401001410 0103BRS
0610431001420 0103BRS
0610905001800 0103BRS

The output should look like this

$$$BRS133010305BRS ACCT FEED * NM05
0610401001410 010305BRS
0610401001410 010305BRS
0610431001420 010305BRS
0610905001800 010305BRS

Can anyone suggest me how to achieve this?

Thanks
Kaan
 
A starting point:
awk '
FNR==1{i=substr($0,14,2);print;next}
{print substr($0,1,24) i substr($0,25)}
' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Code:
awk -f '
1==NR { digits = substr( $0, 14, 2 )
  print; next
}
{ print substr($0,1,24) digits substr($0,25) }
' infile >outfile
 
PHV said:
futurelet, why the -f option ?
A mistake from force of habit. I put my awk programs in files and run them with [tt]awk -f prog.awk[/tt].
Code:
awk '
1==NR { digits = substr( $0, 14, 2 )
  print; next
}
{ print substr($0,1,24) digits substr($0,25) }
' infile >outfile
 
By changing the field separator, we can make every character a field and avoid the ugliness of [tt]substr()[/tt]:
Code:
awk '
BEGIN { FS=OFS="" }
1==NR { digits = $14 $15; print; next }
{ $24 = $24 digits; print }
' infile >outfile
 
Thanks a lot to both of you.

I appreciate your help.
Kaan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top