Search code examples
unixawksedcut

Cut specific columns and collapse with delimiter in Unix


Say I have 6 different columns in a text file (as shown below)

A1  B1  C1  D1  E1  F1
1   G   PP  GG  HH  GG
z   T   CC  GG  FF  JJ

I would like to extract columns first, second and fourth columns as A1_B1_D1 collapsed together and the third column separated by tab.

So the result would be:

A1_B1_D1    C1  
1_G_GG      PP
z_T_GG      CC

I tried

cut -f 1,2,4 -d$'\t' 3, but is just not what I want.


Solution

  • Could you please try following.

    awk '
    BEGIN{
      OFS="\t"
    }
    {
      print $1"_"$2"_"$4,$3
    }
    ' Input_file