Concatenate / Join Columns

File (~ 50,000 columns)

A1 2 123 f f j j k k  
A2 10 789 f o p f m n

      

Output

A1 2 123 ff jj kk  
A2 10 789 fo pf mn

      

I basically want to concatenate every two columns into one, starting at column4. How can we do this in awk or sed?

+3


source to share


1 answer


This is possible in awk. See below

:~/t> more test.txt
A1 2 123 f f j j k k

:~/t> awk '{for(i=j=4; i < NF; i+=2) {$j = $i$(i+1); j++} NF=j-1}1' test.txt
A1 2 123 ff jj kk

      



Sorry you provided two lines as an example ...

:~/t> more test.txt
A1 2 123 f f j j k k
A2 10 789 f o p f m n

:~/t> awk '{for(i=j=4; i < NF; i+=2) {$j = $i$(i+1); j++} NF=j-1}1' test.txt
A1 2 123 ff jj kk
A2 10 789 fo pf mn

      

+1


source







All Articles