Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate/merge 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 from column4. How can we do it in awk or sed?

like image 690
akang Avatar asked Dec 22 '25 08:12

akang


1 Answers

It 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 just noticed you gave two lines as 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
like image 182
SriniV Avatar answered Dec 24 '25 10:12

SriniV



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!