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' ?

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

3 Answers 3

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .