discipulus
discipulus

Reputation: 2725

Append a character to specific multiple columns in specific rows

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

Answers (3)

potong
potong

Reputation: 58528

This might work for you (GNU sed):

sed -i '1s/\S\+/sold&/3g' file

Upvotes: 2

fedorqui
fedorqui

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

Vijay
Vijay

Reputation: 67291

awk 'NR==1{x=$1"\t"$2;for(i=3;i<=NF;i++)x=x"\tsold"$i;$0=x}1' file

Upvotes: 0

Related Questions