0

I have a file A.tsv (field separator : \t) :

BC02    Streptococcus oralis  chromosome, complete genome   2712    94  0   99.073  2053209 CP023507.1  1597
BC02    Staphylococcus aureus  chromosome, complete genome  2712    94  0   99.073  2053209 CP023507.1  1597
BC02    Streptococcus sp.  chromosome, complete genome  2712    94  0   99.073  2053209 CP023507.1  1597

And I want to add a new column at the end of each line with only the first two words of the column $2, in order to get:

BC02    Streptococcus oralis  chromosome, complete genome   2712    94  0   99.073  2053209 CP023507.1  1597    Streptococcus oralis
BC02    Staphylococcus aureus  chromosome, complete genome  2712    94  0   99.073  2053209 CP023507.1  1597    Staphylococcus aureus
BC02    Streptococcus sp.  chromosome, complete genome  2712    94  0   99.073  2053209 CP023507.1  1597    Streptococcus sp.

I try to look at some awk command using this stack subject but nothing similar enough to have a start.

Do you know how to do that ?

4 Answers 4

4

Try to split() the second column on spaces and print how many words do you want, like

awk 'BEGIN{ FS=OFS="\t" }
{ split($2, tmp, " "); print $0, tmp[1], tmp[2] }' infile
1
  • 1
    Thanks ! I will remember using split next time
    – nstatam
    Commented Sep 21, 2021 at 10:17
0

For more complex tsv, e.g. if you had tabs inside fields, awk won't work well. Then, you should use a proper CSV parser, e.g. python's csv module:

#!/usr/bin/env python3
import csv
with open('A.tsv') as csvfile:
    reader = csv.reader(csvfile, delimiter='\t')
    for row in reader:
        row.append(' '.join(row[1].split()[:2]))
        print('\t'.join(row))
0

With GNU awk for gensub() and \s/\S shorthand:

$ awk '{print gensub(/\S+\s+(\S+\s+\S+).*/,"&\t\\1",1)}' file
BC02    Streptococcus oralis  chromosome, complete genome   2712    94  0   99.073  2053209 CP023507.1  1597    Streptococcus oralis
BC02    Staphylococcus aureus  chromosome, complete genome  2712    94  0   99.073  2053209 CP023507.1  1597    Staphylococcus aureus
BC02    Streptococcus sp.  chromosome, complete genome  2712    94  0   99.073  2053209 CP023507.1  1597        Streptococcus sp.

or a bit briefer with GNU sed:

$ sed -E 's/\S+\s+(\S+\s+\S+).*/&\t\1/' file
BC02    Streptococcus oralis  chromosome, complete genome   2712    94  0   99.073  2053209 CP023507.1  1597    Streptococcus oralis
BC02    Staphylococcus aureus  chromosome, complete genome  2712    94  0   99.073  2053209 CP023507.1  1597    Staphylococcus aureus
BC02    Streptococcus sp.  chromosome, complete genome  2712    94  0   99.073  2053209 CP023507.1  1597        Streptococcus sp.

The above assumes the first field doesn't contain any white space, as shown in your examples.

0

Using Raku (formerly known as Perl_6)

raku -ne 'print $_, "\t"; .split(/\t/).[1].words.[0..1].put;' 

Sample Input:

BC02    Streptococcus oralis  chromosome, complete genome   2712    94  0   99.073  2053209 CP023507.1  1597
BC02    Staphylococcus aureus  chromosome, complete genome  2712    94  0   99.073  2053209 CP023507.1  1597
BC02    Streptococcus sp.  chromosome, complete genome  2712    94  0   99.073  2053209 CP023507.1  1597

Breaking the code above into three (3) parts,

1). split on tabs, pull out the second element (remembering that in Raku numbering starts from zero):

raku -ne '.split(/\t/).[1].put;'

gives Sample Output:

Streptococcus oralis  chromosome, complete genome
Staphylococcus aureus  chromosome, complete genome
Streptococcus sp.  chromosome, complete genome

2). Break into whitespace-separated words, take the first two (2):

raku -ne '.split(/\t/).[1].words.[0..1].put;'

gives Sample Output:

Streptococcus oralis
Staphylococcus aureus
Streptococcus sp.

3). Combine above with the entire pre-existing line, by printing the Raku $_ topic variable first (followed by a \t):

raku -ne 'print $_, "\t"; .split(/\t/).[1].words.[0..1].put;'  

gives Sample Output:

BC02    Streptococcus oralis  chromosome, complete genome   2712    94  0   99.073  2053209 CP023507.1  1597    Streptococcus oralis
BC02    Staphylococcus aureus  chromosome, complete genome  2712    94  0   99.073  2053209 CP023507.1  1597    Staphylococcus aureus
BC02    Streptococcus sp.  chromosome, complete genome  2712    94  0   99.073  2053209 CP023507.1  1597    Streptococcus sp.

https://raku.org/

You must log in to answer this question.

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