Concatenate two columns in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatenate two columns in a file
# 8  
Old 08-11-2014
Hello,

Could you please try the following for same.

Code:
awk -F, '{$(NF-1)=$(NF-1) $NF}; {a=(NF-1)} {if(NR==1){$(NF-1)="new column"}}  {if(NR==1){$NF="new column"}} {for(i=1;i<=(NF-1);i++){if(i%a==0){ORS="\n"; print $i}  else {ORS=","; print $i}}}' OFS="," filename

Output will be as follows.

Code:
COL1,COL2,new column
1,A,B,CD
2,e,f,gh


Thanks,
R. Singh

Last edited by RavinderSingh13; 08-11-2014 at 10:47 AM.. Reason: a small correction
# 9  
Old 08-11-2014
From Singh's code I have learnt a few things like OFS,ORS,NF,NR,FS

Thanks Singh.

@Bhaskar

In my previous code it is feasible if you have less number of columns. But what if you have more columns in your input file? It is difficult to enter all the numbers one by one in the code. That's why I have changed my code using the Singh's code and finally arrived with the output

Try this code
Code:
awk -F, '{for(i=1;i<=(NF-1);i++){if(i==(NF-1)){ORS="\n"; print $i$(i+1)}  else {ORS=","; print $i}}}' OFS=","  test250

Output:
Code:
Col1,Col2,Col3Col4
1,A,B,CD
2,e,f,gh


Note: Just change the first line of output in your text editor



Moderator's Comments:
Mod Comment Please wrap [CODE] & [/CODE] tags round code, input & output/errors to make them easier to read.
# 10  
Old 08-11-2014
Try also:
Code:
awk 'NR==1 {print $1, $2, "newcolumns"; next} {$4=$4$5;sub(","$5,"",$0)}1' FS=, OFS=, file
COL1,COL2,newcolumns
1,A,B,CD
2,e,f,gh

or, for more than 5 columns:
Code:
 awk 'NR==1 {print $1, $2, "newcolumns"; next} {$(NF-1)=$(NF-1)$NF;sub(","$NF,"",$0)}1' FS=, OFS=, file4
COL1,COL2,newcolumns
1,A,B,CD
2,e,f,gh

or, to make it even more versatile (for the header):
Code:
awk '{$(NF-1)=NR==1?hd:$(NF-1)$NF;sub(","$NF,"",$0)}1' FS=, OFS=, hd="newcolumns" file
COL1,COL2,newcolumns
1,A,B,CD
2,e,f,gh


Last edited by RudiC; 08-11-2014 at 09:48 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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... (10 Replies)
Discussion started by: huiyee1
10 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. 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

6. Shell Programming and Scripting

Concatenate strings retrieved from a file and write it into another file

Hi, I have a file files.txt containing data as below: abc;xyz uvw;pqr 123;456 I want to develop strings like below using the above data and write them into another file: www/xxx/abc/yyy/xyz www/xxx/uvw/yyy/pqr www/xxx/123/yyy/456 All this needs to be done through .sh file. ... (4 Replies)
Discussion started by: archana.n
4 Replies

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

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

9. Shell Programming and Scripting

Insert file names when concatenate files into a file

Hi I found the following line would concatenate all test_01 test_02 test_03 files into "bigfile". cat test_* >> bigfile But, what I'm looking for a way to insert each file names in order when concatenated in "bigfile". Thank you samky2005 (2 Replies)
Discussion started by: samky2005
2 Replies

10. UNIX for Dummies Questions & Answers

file Concatenate

Hi All, I have a question about file concatenate on unix. I have two file 1 of them like aaa,bbb,ccc,ddd other one is eee,fff,ggg,hhh I want to concatenate those file like this position aaa,bbb,ccc,ddd,eee,fff,ggg,hhh how can I do this ?? thanks. Alice (3 Replies)
Discussion started by: alisev
3 Replies
Login or Register to Ask a Question