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!

Increment a field.

Status
Not open for further replies.

dbehera

Programmer
May 31, 2005
8
US
I am new and have a data file with the 16th column as

0000779TES and have 1000 records

0000779TES
0000779TES
0000779TES
0000779TES


I want to change it to

0000100TES
0000101TES
0000102TES
.
.
.
0000998TES
0000999TES


How can I replace it with increment from 779 to 100,101,102,...

Thanks



 
Hi

For strictly that you wrote :
Code:
awk '{$16=sprintf("%07dTES",NR+99); print}' inputfile > outputfile
But supposing that there are also lines without 0000999TES :
Code:
awk '$16=="0000999TES" {$16=sprintf("%07dTES",nr+++100); print}' inputfile > outputfile

Feherke.
 
Thanks this works but the output changes the format of the file.

I had

TRX111161500 0365A12005090201 355 PMD D 2C 20050900 724 FDJ 00000000002222+00001K DB01C 4 2 DB0C
-890 000779TES Q AS A9994 RTH S1CAL 00000000000110- 1 20051200 00000000002332+000
01 R1 093618 S

Now all the blanks goes off and it comes as
TRX111161500 0365A12005090201 355 PMD D 2C 20050900 724 FDJ 00000000002222+00001K DB01C 4 2 DB0C -890 0000989TES Q AS A9994 RTH S1CAL 00000000000110- 1 20051200 00000000002332+00001 R1 093618 S


It removes all the necessary blanks and keeps it as 1 blank between fields.

Is there another way of doing it maybe with sed.

 
awk '
{x=$0;sub(/ 0000779TES /,sprintf(" %07dTES ",NR+99),x);print x}
' /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 FAQ181-2886
 
This should work but i am getting a syntax error. Not able to figure out why.

awk: syntax error near line 1
awk: illegal statement near line 1
 
Which awk ? which OS ? which EXACT code have you tried ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi

dbehera, you said nothing about field separators. Neighter in your second post. The sample looks like tab separated, so could help to set the output field separator to tab :
Code:
awk '[red]BEGIN { OFS="\t" }[/red] $16=="0000999TES" {$16=sprintf("%07dTES",nr+++100); print}' inputfile > outputfile

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top