awk

using awk to converting a column of data into a row


I'm trying to convert a columns of data to a row using nawk

input=
1775
1800
1825
200
2850
3050
3075
3175

my code is:

nawk 'BEGIN{ORS=","}{print $0}'

but output is not included all data set in my input file. can anyone help me through this...?

output:

,2850,3050,3075,3175,

Solution

  • Could you please try following.

    awk -v OFS="," '
    { 
      gsub(/\r/,"")
      val=(val?val OFS:"")$0
    }
    END{
      print val
    }
    '  Input_file
    

    By looking OP's attempt it looks like OP may be on Sun o.s Solaris system, if this is the case change awk to nawk

    Explanation:

    Setting OFS as , to get output in comma separated values. Creating variable named val whose value is concatenating of its own value each time with comma. In END section of this code printing variable val to get all lines comma separated as per OP requirements.



    Not tested on Sun o.s try if you have paste command there

    tr -d '\r' < Input_file > temp && mv temp Input_file;paste -sd',' Input_file