awk

Concatenate all fields after specific one in a file with variable NF


I have a file like this:

ID1 as;uh;2
ID2 uh;3 jk PASS
ID3 PASS
ID4 as;uh;PASS kk;3 rt
ID5 as
ID6 PASS PASS uh 3;4
ID7 jk;34 hy

I would like to concatenate all fields after the first, separating them by | and getting the following:

ID1 as;uh;2
ID2 uh;3|jk|PASS
ID3 PASS
ID4 as;uh;PASS|kk;3|rt
ID5 as
ID6 PASS|PASS|uh|3;4
ID7 jk;34|hy

I was trying something like this, but I am not sure how to automatically concatenate all columns after the first one, instead of manually indicating the number of columns (because in my real file, some rows present more than 15 fields).

awk -v OFS='\t' '{ $2=$2"|"$3"|"$4;    #merge cols after the first one   
        for (i=3;i<NF;i++) $(i)=""} 1'    #remove cols after 2nd one and print it                      

Do you know how can I achieve it?


Solution

  • With GNU sed:

    $ sed 's/ /|/2g' ip.txt
    ID1 as;uh;2
    ID2 uh;3|jk|PASS
    ID3 PASS
    ID4 as;uh;PASS|kk;3|rt
    ID5 as
    ID6 PASS|PASS|uh|3;4
    ID7 jk;34|hy
    

    2g will replace only from the second match onwards.