-1

Hi I am looking for a command line solution to replace the '1' value in each line I have with ascending values. My input looks like:

K   X   1
K   X   1
K   X   1
K   X   1
K   X   1
K   X   1
K   X   1
K   X   1
K   X   1
K   X   1

and I would like the output to be

K   X   1
K   X   2
K   X   3
K   X   4
K   X   5
K   X   6
K   X   7
K   X   8
K   X   9
K   X  10

Any help would be much appreciated.

Thanks

  • 3
    What have you tried? Are the numbers always the last field of the file? The number 10 was displaced to the left: Is that necessary/correct or a mistake? – Quasímodo Jan 30 '21 at 23:16

4 Answers4

4

This may be an option with awk:

$ awk '$3 = $3+i++' file
K X 1
K X 2
K X 3
K X 4
K X 5
K X 6
K X 7
K X 8
K X 9
K X 10

2

If you want lines of input to be renumbered sequentially, you can use the awk record number, NR ex.

awk '{printf "%s%4s%4d\n", $1, $2, NR}' input
steeldriver
  • 81,074
0

My Bash solution (untested, and it assumes that K and X don't contain white space):

i=1
while read a b c
do 
  echo $a $b $i
  i=$((i+1))
done < inputfile

Correct output formatting is left as an exercise to the reader.

berndbausch
  • 3,557
0
$ awk '$3=NR' file
K X 1
K X 2
K X 3
K X 4
K X 5
K X 6
K X 7
K X 8
K X 9
K X 10

Fore! :-)

Ed Morton
  • 31,617