How to concatenate 2-columns by 2 -columns for a text file?

Hello,

I want to concatenate 2-columns by 2-columns separated by colon. How can I do so? For example, I have a text file containing 6 columns separated by tab. I want to concatenate column 1 and 2; column 3 and 4; column 5 and 6, respectively, and put a colon in between.

input file:

1 0 0 1 1 1
0 1 1 1 0 0
1 1 0 0 1 1
1 0 0 1 0 1
0 1 0 0 1 0
1 1 1 1 1 1
0 0 0 0 0 0

output:

1:0 0:1 1:1
0:1 1:1 0:0
1:1 0:0 1:1
1:0 0:1 0:1
0:1 0:0 1:0
1:1 1:1 1:1
0:0 0:0 0:0

Thanks in advance.

Hello,

Following may help you in same.

awk '{$1=$1":"$2} {$3=$3":"$4} {$5=$5":"$6} {print $1" "$3" "$5}' concatenate_file

Output will be as follows.

1:0 0:1 1:1
0:1 1:1 0:0
1:1 0:0 1:1
1:0 0:1 0:1
0:1 0:0 1:0
1:1 1:1 1:1
0:0 0:0 0:0

Thanks,
R. Singh

Another one:

awk '{$1=$1 ":" $2; $2=$3 ":" $4; $3=$5 ":" $6} NF=3' file

overly simplified:

awk '{print $1":"$2,$3":"$4,$5":"$6}' <input file>

Dear all,

Thank for the code. I know it can be done with awk. However, I have to concatenate for 200,000 columns. Is there any code to concatenate all 200,000 columns?

Many thanks.:slight_smile:

Try this:

sed 's/\([0-9]\) \([0-9]\)/\1:\2/g' file

Dear Subbeh,

Thanks for your help. But, it does not work.

You can try something like:

awk '{for(i=1;i<=NF;i+=2)printf $i ":" $(i+1) FS}{print ""}' file
1 Like

What doesn't work? It works with the sample you provided:

sed 's/\([0-9]\) \([0-9]\)/\1:\2/g' file
1:0 0:1 1:1
0:1 1:1 0:0
1:1 0:0 1:1
1:0 0:1 0:1
0:1 0:0 1:0
1:1 1:1 1:1
0:0 0:0 0:0

The command does assume that the collumns contain only single digit numbers.

1 Like

Dear all,

I figure it out with perl

perl -pe 's/([^\s]+)\s+([^\s]+)/$1:$2/g' input_file

---------- Post updated at 08:28 AM ---------- Previous update was at 08:21 AM ----------

Dear Subbeh,

I found out my mistake. The text file I posted was separated by space while my original file was separated by tab. Your script works too. Thank you very much. :slight_smile:

Hello All,

Following may help too.

awk '{for(i=1;i<=NF;i++) {if(i%2!=0) {a=a" "$i":"$(i+1)} else{if(i==NF) {print a;a=""} }}}' file_name

Output will be as follows.

 1:0 0:1 1:1 2:2
 0:1 1:1 0:0 3:3
 1:1 0:0 1:1 4:4
 1:0 0:1 0:1 5:5
 0:1 0:0 1:0 6:6
 1:1 1:1 1:1 7:7
 0:0 0:0 0:0 8:8

Thanks,
R. Singh

1 Like