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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to concatenate 2-columns by 2 -columns for a text file?
# 8  
Old 03-05-2014
You can try something like:
Code:
awk '{for(i=1;i<=NF;i+=2)printf $i ":" $(i+1) FS}{print ""}' file

This User Gave Thanks to Franklin52 For This Post:
# 9  
Old 03-05-2014
Quote:
Originally Posted by huiyee1
Dear Subbeh,

Thanks for your help. But, it does not work.
What doesn't work? It works with the sample you provided:
Code:
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.

Last edited by Subbeh; 03-05-2014 at 09:22 AM..
This User Gave Thanks to Subbeh For This Post:
# 10  
Old 03-05-2014
Dear all,

I figure it out with perl
Code:
perl -pe 's/([^\s]+)\s+([^\s]+)/$1:$2/g' input_file

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

Quote:
Originally Posted by Subbeh
What doesn't work? It works with the sample you provided:
Code:
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.

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. Smilie

Last edited by huiyee1; 03-05-2014 at 10:36 AM.. Reason: Please use code tags
# 11  
Old 03-05-2014
Hello All,

Following may help too.


Code:
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.

Code:
 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
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Concatenate two columns in a file

Hi i am having one requirement like to concatenate two columns in to one columns, can any one help on this please sample scenario i am placing below COL1,COL2,COL3,COL4 1,A,B,C,D 2,e,f,g,h ouput should be 3 columns like below. COL1,COL2,newcolumns 1,A,B,CD 2,e,f,gh ... (9 Replies)
Discussion started by: bhaskar v
9 Replies

2. Shell Programming and Scripting

Grouping multiple columns and concatenate

I have a CSV file that goes like this: Name,Group,Email Max,Group1,max@.com Dan,Group2,dan@.com Max,Group3,max@.com Max,Group4,max@.com Dan,Group5,dan@.com Jim,Group6,jim@.comBasically my desired output should be: Name,Group,Email Max,Group1|Group3|Group4,max@.com... (6 Replies)
Discussion started by: jeffreybsu
6 Replies

3. Shell Programming and Scripting

Deleting all the fields(columns) from a .csv file if all rows in that columns are blanks

Hi Friends, I have come across some files where some of the columns don not have data. Key, Data1,Data2,Data3,Data4,Data5 A,5,6,,10,, A,3,4,,3,, B,1,,4,5,, B,2,,3,4,, If we see the above data on Data5 column do not have any row got filled. So remove only that column(Here Data5) and... (4 Replies)
Discussion started by: ks_reddy
4 Replies

4. UNIX for Dummies Questions & Answers

Concatenate two columns and separate by - (minus)

hi all could you please help me to concatenate two colomns and separate them by "-" the two colomns to concatenate are colomuns 1 and 3 of a very bif file clomn 1 is chr, 2 is snp and 3 is bp the new colomn is chr_B input file : 1 rs1111 10583 1 rs1891 10611 1 rs1807 ... (13 Replies)
Discussion started by: biopsy
13 Replies

5. UNIX for Advanced & Expert Users

Help in Deleting columns and Renaming Mutliple columns in a .Csv File

Hi All, i have a .Csv file in the below format startTime, endTime, delta, gName, rName, rNumber, m2239max, m2239min, m2239avg, m100016509avg, m100019240max, metric3min, m100019240avg, propValues 11-Mar-2012 00:00:00, 11-Mar-2012 00:05:00, 300.0, vma3550a, a-1_CPU Index<1>, 200237463, 0.0,... (9 Replies)
Discussion started by: mahi_mayu069
9 Replies

6. Shell Programming and Scripting

Concatenate columns from multiple files

Hi all, I want the 2nd column of every file in the directory in a single file with the file name as column header. $cat file1.txt a b c d e f $cat file2.txt f g h g h j $cat file3.txt a b d f g h (2 Replies)
Discussion started by: newbie83
2 Replies

7. UNIX for Dummies Questions & Answers

Removing columns from a text file that do not have any values in second and third columns

I have a text file that has three columns. But at the end of the text file, there are trailing lines that have missing second and third columns: 4 0.04972604 KLHL28 4 0.0497332 CSTB 4 0.04979822 AIF1 4 0.04983331 DECR2 4 0.04990344 KATNB1 4 4 4 4 How can I remove the trailing... (3 Replies)
Discussion started by: evelibertine
3 Replies

8. Shell Programming and Scripting

Concatenate columns from files

Hi, I have a file1.txt like this: and a file2.txt like this: I wat only append file2.txt to file1.txt and to have something like this: How could i do ? (2 Replies)
Discussion started by: AdminLew
2 Replies

9. Shell Programming and Scripting

concatenate 'n' number of columns in a file

i have a file which may have 'n' number of columns 1 222 fafda 32 afdaf 4343 4343 234 43fdaf 4343 fdd fdfd fdfd fdd fdfd fdfd fdfd fdfd fdfd fdd fdfd fdfd need to concatenate the columns with... (3 Replies)
Discussion started by: mlpathir
3 Replies

10. UNIX for Dummies Questions & Answers

How to convert text to columns in tab delimited text file

Hello Gurus, I have a text file containing nearly 12,000 tab delimited characters with 4000 rows. If the file size is small, excel can convert the text into coloumns. However, the file that I have is very big. Can some body help me in solving this problem? The input file example, ... (6 Replies)
Discussion started by: Unilearn
6 Replies
Login or Register to Ask a Question