Reputation: 1783
Assume a text file file that contains multiple lines of text strings.
> cat file
foo bar baz
bar quux foo
foo baz quux
I would like to add a consecutive number (with leading digits, followed by an underscore) to each third-column word.
> cat file | sought_command
foo bar 001_baz
bar quux 002_foo
foo baz 003_quux
I believe that this can be achieved simply via awk (awk '{printf("%03d ...
), but can't come up with the proper code.
Upvotes: 2
Views: 82
Reputation: 11216
You can use sprintf and assign it to $3, using the line number as the digit.
awk '$3=sprintf("%03d_%s",NR,$3)' file
foo bar 001_baz
bar baz 002_qux
foo baz 003_quux
Upvotes: 3