Search code examples
linuxbashsh

How to merge multiple columns in a file to a single column using bash commands?


I have a text file with three different columns . I want to create another file by merging all these columns into a single column.

my file looks like this

mep_kylo_campaigns               mep_primecastaccount        mep_flightstatus
nqs                              tod_do                      gandhi_sub_data 
kylo_register                    policy_record               mep_kylo_jobs   
mep_note                         msg_store                   mep_feature     
nqs_aside                        tbl_employee                mep_profile  

i want my output like this

mep_kylo_campaigns                       
nqs                                                     
kylo_register                                      
mep_note                                                 
nqs_aside                                           
mep_primecastaccount
mep_flightstatus
tod_do
policy_record
msg_store
tbl_employee
gandhi_sub_data
mep_kylo_jobs
mep_feature
mep_profile

Solution

  • If you are interested in doing it awk this is the way :

    awk 'BEGIN{ ORS="" } { for ( i=1; i<= NF ; i++){ print $i"\n"  }  }' input.txt
    

    Additionally, if you are seeking to preserve the order of the columns you can use this :

    awk 'BEGIN{ ORS="" } { for ( i=1; i<= NF ; i++){ dict[i]=dict[i]$i"\n"  }  } END { for (key in dict) { print dict[key] }  }' input.txt
    

    Hope it helps!