Search code examples
bashawkgrepcut

Bash: extract columns with cut and filter one column further


I have a tab-separated file and want to extract a few columns with cut.

Two example line

(...)
0    0    1    0    AB=1,2,3;CD=4,5,6;EF=7,8,9    0    0
1    1    0    0    AB=2,1,3;CD=1,1,2;EF=5,3,4    0    1
(...)

What I want to achieve is to select columns 2,3,5 and 7, however from column 5 only CD=4,5,6.

So my expected result is

0    1    CD=4,5,6;    0
1    0    CD=1,1,2;    1

How can I use cut for this problem and run grep on one of the extracted columns? Any other one-liner is of course also fine.


Solution

  • here is another awk

    $ awk -F'\t|;' -v OFS='\t' '{print $2,$3,$6,$NF}' file
    
    0       1       CD=4,5,6        0
    1       0       CD=1,1,2        1
    

    or with cut/paste

    $ paste <(cut -f2,3 file) <(cut -d';' -f2 file) <(cut -f7 file)
    
    0       1       CD=4,5,6        0
    1       0       CD=1,1,2        1