Increment existing column in file

Hi All,
I have a file with 3 millions records in which 3rd column is same throughout say its value is 0 throughout.for example:

 
Col1 Col2 Col3 Col4
A     1      0     5
B     2      0     6
C     3      0     7
D     4      0     9

I want my output as :

 
Col1 Col2 Col3 Col4
A     1      1     5
B     2      2     6
C     3      3     7
D     4      4     9

Can anybody please help me with this.I want to use for loop in my shell.

A few questions first:-

  • Would you want this coded in shell script entirely so you could add more things into the loop, or would an awk one-liner be okay?
  • Do you have data in fixed width columns? If so, what are the requirements?
  • What OS are you using? Some commands can be flavour or even version specific.
  • What shell are you using? ksh, bash, csh perhaps?
  • Why does the row starting D not have an 8 in Col4?

Okay, ignore that last one.

I'm trying to understand if you want to have a one-off conversion of the file or are you wanting to perform some action using the data on each line as you go.

Thanks, in advance,

Robin
Liverpool/Blackburn
UK

awk 'NR==1{$1=$1}NR>1{$3=++c}1' OFS='\t' file
1 Like

Hi..Thank u so much for quick response..
Answers to ur questions:
I am using ksh scripts and just wanted one liner command line command to increment third column.
I got the required code from unix forum..:slight_smile: as below::

 
awk -F"," -v OFS="," 'NR>1{ $3+=int((NR-1)/1) } 1' infile > outfile

thank you unix forum..:slight_smile:

awk 'NR>1 {$3=NR-1} 1' file
Col1 Col2 Col3 Col4
A 1 1 5
B 2 2 6
C 3 3 7
D 4 4 9
1 Like