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!

awk and line numbering 2

Status
Not open for further replies.

kaanlu

Programmer
Jan 13, 2005
9
US
Hi,

I have an awk command that modifies the raw batch file.I would like to add a command to insert a unique sequencial number at the end of each line in the batch file. Does any one know how I can achieve it or what command I need to use?

Here is the awk command :
$awk '
1==NR {digits = substr($0,14,2)
point = "."}
{print substr($0,1,24) digits substr($0,25,29) point substr($0,54)} ' misc_interface.out > misc_interface.dat

The last field at the end of each line is 126th.I want to add 1,2,3, etc sequencial numbers after this field.

Thanks.
 
{print substr(print substr($0,1,24) digits substr($0,25,29) point substr($0,54) NR}

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 '
BEGIN { FS=OFS="" }
1==NR { digits = $14 $15; print; next }
{ $24 = $24 digits; $53 = $53 "."
  print $0 NR-1 }
' infile >outfile
 
PHV => {print substr(print substr($0,1,24) digits substr($0,25,29) point substr($0,54) NR} did not work. I goy "illegal statement, syntax error message.
 
Oops, copy'n'paste error, my bad:
{print substr($0,1,24) digits substr($0,25,29) point substr($0,54) NR}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV =>
Thanks but this creates sequencial number at a seperate line. I want to add this number at the end of each line.

This is the output of your command.
1
0610401001410 ...... 367133 0905001800 J3 05

I want this number (1,2,3) at the end of each line like

0610401001410 ...... 367133 J3 05 1
0610401001410 ...... 367133 J3 05 2
0610401001410 ...... 367133 J3 05 3

Futurelet=> I did not test yours yet.
 
????
$awk '
1==NR {digits = substr($0,14,2)
point = "."}
{print substr($0,1,24) digits substr($0,25,29) point substr($0,54)" "NR} ' misc_interface.out > misc_interface.dat


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV=> Sorry same result. I see the sequencial numbers at seperate lines again. Maybe I should try to put the sequencial numbers into empty fields in each line like somewhere between 77th and 81th fields. Maybe adding the numbers at the end of record does not work!
 
Seems you have funny control char at the end of the input line.
The last field at the end of each line is 126th
$awk '
1==NR{digits=substr($0,14,2);point="."}
{print substr($0,1,24) digits substr($0,25,29) point substr($0,54[highlight],73[/highlight])" "NR}
' misc_interface.out > misc_interface.dat

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV=> Server is down. I will test it first thing tomorrow morning and post the result here. Pls check my reply again tomorrow morning. Thanks.

Futurelet=> I will test yours tomorrow too. Thanks for your input.
 
Is the first line of the file batch-header info? I presume that it is. With this input:

$$$BRS133010305BRS ACCT FEED * NM05
0610905001800 0103BRS abcdefghijklmnopqrstu 1234567890 --1 --2 --3 --4 --5 --6 --7 --8 --9 --0 367133 J3 05
0610905001800 0103BRS abcdefghijklmnopqrstu 1234567890 --1 --2 --3 --4 --5 --6 --7 --8 --9 --0 367133 J3 05

the ouput is

$$$BRS133010305BRS ACCT FEED * NM05
0610905001800 010305BRS abcdefghijklmnopqrstu 123.4567890 --1 --2 --3 --4 --5 --6 --7 --8 --9 --0 367133 J3 05 1
0610905001800 010305BRS abcdefghijklmnopqrstu 123.4567890 --1 --2 --3 --4 --5 --6 --7 --8 --9 --0 367133 J3 05 2

Code:
BEGIN { FS=OFS="" }
# Make sure line isn't longer than 126 characters.
{ $0 = substr( $0, 1, 126 ) }
# First line is the batch header info?
1==NR { digits = $14 $15; print; next }
{ $24 = $24 digits; $53 = $53 "."
  print $0 " " NR-1 }
 
futurelet => I got
ksh: syntax error: `(' unexpected
error.
Yes, first line is the batch header info.I used the same code.


PHV=> I still do not get the line number at the end.

Input file:
This is batch header info:
$$$BRS133010305BRS ACCT FEED * 0009709772683404 NM05

This the first dataline record:
0610401001410 0103BRS ACCOUNTING FEED 00144731900 367133 0905001800 J3 05

I have three batches and 200 something records in the file.

 
Save the awk program aw modify.awk.
Run it with:
awk -f modify.awk misc_interface.out > misc_interface.dat
 
thanks to both of you. it worked.

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

Part and Inventory Search

Sponsor

Back
Top