Reputation: 2725
I have a file I want to add specific character to multiple specific columns in specific rows. For eaxample my file is
shirts pants tshirts greet
100000 11111 1200000 131313
123456 15823 1542358 752256
I want to add a character sold to all columns after second (there can be multiple columns, unknown) to only the first row. I can select first row using and columns using
NR<2 and NF>2
But how do I append to each fields? I want my answer to be
shirts pants soldtshirts soldgreet
100000 11111 1200000 131313
123456 15823 1542358 752256
Upvotes: 1
Views: 200
Reputation: 290255
You were right about the use of NR<2
and NF>2
. Something like this can be a solution:
$ awk 'NR==1{for (i=3; i<=NF; i++) $i="sold"$i}1' a
shirts pants soldtshirts soldgreet
100000 11111 1200000 131313
123456 15823 1542358 752256
On NR==1
(first line) it loops from 3rd to last field appending "sold" to each one. Then the 1
evaluates as true and prints the whole line in all cases.
Upvotes: 2