Yarden Hochenberg
Yarden Hochenberg

Reputation: 21

Add a leter to the 46th word in every line in a text file using linux

I have a text file with 500k rows. In each line there are 47 words.
I want to add the letter 'C' to the 46th word in every line except the first line using linux (the first line is the table title).

For example:
ID FID IID ..... number_center age
1 1001 807 ..... 10960 47
3 900 818 ..... 10877 51

The output: new text file
ID FID IID ..... number_center age
1 1001 807 ..... C10960 47
3 900 818 ..... C10877 51

I tried to find the answer but did not find one.
Thank you

Upvotes: 0

Views: 32

Answers (1)

Raman Sailopal
Raman Sailopal

Reputation: 12877

Using awk, set the 46th space delimited field (46th word) to "C" plus the 46th field. Print the line with shorthand 1. Ignore the first line with NR>1.

awk 'NR>1 { $46="C"$46 }1' filename

Upvotes: 3

Related Questions