Home > Mobile >  Add prefix to the 3rd word in every line
Add prefix to the 3rd word in every line

Time:12-16

I have an input file.

aaa bbb ccc ddd
123 456 789 11223
abc efg hijk lmno
111 222 333 444

I want to add "AA_" to every 3rd word in each line and I need an output file as below

aaa bbb AA_ccc ddd
123 456 AA_789 11223
abc efg AA_hijk lmno
111 222 AA_333 444

CodePudding user response:

Assuming the input file is:

$ cat infile
aaa bbb ccc ddd 123 456 789 11223
abc efg hijk lmno 111 222 333 444 555

the following awk script will produce the desired output by prefixing AA_ to every third field:

$ awk '{for (i=1;i<=NF;i  )if(i%3==0)$i="AA_"$i; print $0}' infile
aaa bbb AA_ccc ddd 123 AA_456 789 11223
abc efg AA_hijk lmno 111 AA_222 333 444 AA_555

CodePudding user response:

sed -E 's/[^ ] /AA_&/3' input_file
  • s///3 will do replacement on the 3rd occurence of the match
  • & in the replacement side means "everything matched"
  • I am using [^ ] with sed -E to mean "a word."
  • Requires GNU sed, I believe

CodePudding user response:

A couple of more awks. To output records with less than 3 words or adding the prefix to the third word and then outputing:

$ awk 'NF<3||$3="AA_" $3' file

or if your data has some weird spacing (multiple spaces between words) you'd want to keep, add the prefix after everything before the third word or output as-is if less than 3 words:

$ awk 'gsub(/^([^ ] [ ] ){2}/,"&AA_")||1' file
  • Related