0

If I have a tab-delimited file containing the following lines:

Sample1-Baboon  1  2  3  4
Sample2-Human   1  2  3  4
Sample3-Human   1  2  3  4
Sample4-Baboon  1  2  3  4

How can I add a new column at the end of the line for those lines which contain the string 'Baboon' ?

  • awk -v OFS='\t' '/Baboon/{$(NF+1)=5}1' – Costas Feb 9 '15 at 22:00
  • what do you want in the new column? – mikeserv Feb 9 '15 at 22:37
1

If the file is named baboon.txt:
sed '/Baboon/ s/$/ Baboon/' baboon.txt

To write the changes to file use the -i option.

sed -i '/Baboon/ s/$/ Baboon/' baboon.txt

1
sed -i '/Baboon/ s/$/\t5/' your_file

This will search your_file for the lines containing Baboon and replace the last character on those lines with a tab character followed by a 5. The -i will write the changes to the file in-place.

0

You can use Vim in Ex mode:

ex -sc 'g/Baboon/s/$/\t5/' -cx file
  1. g global regex

  2. s substitute

  3. x save and close

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.