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

Thanks in advance.

One way:

awk -F, '{$4=$4 $5;NF=4}1' OFS=, file

sorry it's not working

its just displaying the file content as output

awk -F, '{$4=$4 $5;NF=4}1' OFS=, /home/dsadm/rpt/dstempdir/SCRIPT_SEQ_LOG/test.txt

Hello,

Here is one more solution for same.

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

output will be as follows.

COL1,COL2,COL3,COL3COL4
1,A,B,C,CD
2,e,f,g,gh

Thanks,
R. Singh

thanks Singh...

but i dont want to display the COL3 and COL4 values. i Just want to display the new concatenated column only

I am not an expert in unix to tell the most effective way to get your result. But hope this will help you as a temporary fix. Try the below code

awk -F, '{print $1","$2","$3","$4$5}' <filename>

I have tried in my unix box and have got the below output which is same as you mentioned

Col1,Col2,Col3,Col4
1,A,B,CD
2,e,f,gh
1 Like

Hello Bhaskar,
This is what you need??

$ cat file3
COL1,COL2,COL3,COL4,COL5
1,A,B,C,D
2,e,f,g,h
$ cat file3 | awk -F "," '{out=$1; for(i=2;i<=NF-2;i++){out=out","$i}; print out","$(NF-1)$NF}'
COL1,COL2,COL3,COL4COL5
1,A,B,CD
2,e,f,gh

If you always want to join last two columns in your file then you can use above.

1 Like

Hello,

Could you please try the following for same.

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.

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

Thanks,
R. Singh

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

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:

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

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

Try also:

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:

 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):

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