Find the answer to your Linux question:
Results 1 to 7 of 7
Dear AWK gurus, We have a file , "xxx.dat" like this -50.4838 2.6147 0.0827 -49.1492 1.5374 0.1567 -48.1492 3.5374 2.0567 We try to transform them into csv format: -50.4838,2.6147,0.0827 -49.1492,1.5374,0.1567 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2012
    Posts
    24

    awk commas separation


    Dear AWK gurus,

    We have a file , "xxx.dat" like this

    -50.4838 2.6147 0.0827
    -49.1492 1.5374 0.1567
    -48.1492 3.5374 2.0567

    We try to transform them into csv format:

    -50.4838,2.6147,0.0827
    -49.1492,1.5374,0.1567
    -48.1492,3.5374,2.0567

    We have tried the followings. Both do not change the format from "xxx.dat" at all.

    awk -F, '{print $1 $2 $3}' xxx.dat > xxx.csv

    awk -F "\"*,\"*" '{print $1 $2 $3}' xxx.dat > xxx.csv

    Could gurus tell us the reason? Thanks

  2. #2
    Linux Newbie
    Join Date
    Jun 2012
    Posts
    100
    Why not use sed for that?

    sed 's/ /,/g' xxx.dat > xxx.csv

  3. #3
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,442
    awk works as well
    Code:
    awk '{ print $1 "," $2 "," $3  }' < xxx.dat > xxx.csv
    However, both variants are not very resilient against malformed data.
    You probably want to add some regex to ensure data integrity.
    You must always face the curtain with a bow.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    238
    hi,
    Code:
     awk '{$1=$1}1' OFS=',' file

  6. #5
    Just Joined!
    Join Date
    Aug 2012
    Posts
    24
    Quote Originally Posted by watael View Post
    hi,
    Code:
     awk '{$1=$1}1' OFS=',' file

    Thanks Gurus. They work pretty well.

    One more questions

    We would like to print out formats like this:


    91,-50.4838,2.6147,0.0827
    135,-49.1492,1.5374,0.1567
    138,-48.1492,3.5374,2.0567

    Because this scrip is going to process many file again and again. We hope in each csv, the first line will will start with "91", the second line will start with "135", the third line starts with "138"...etc.

    Here is the script

    Y1='91'
    Y2='135'
    Y3='138'

    for X in 1 2 3

    do

    Z="Y$X"
    G=$(eval echo \$$Z)

    awk '{ print "${G}", $1 "," $2 "," $3 }' < mmPBSA_e1_${time1}.dat > mmPBSA_e1_${time1}.csv


    However, awk will print out something like this

    ${G} -50.4838,2.6147,0.0827
    ${G} -49.1492,1.5374,0.1567
    ${G} -48.1492,3.5374,2.0567


    instead of

    91,-50.4838,2.6147,0.0827
    135,-49.1492,1.5374,0.1567
    138,-48.1492,3.5374,2.0567

    Could gurus kindly offer comments? Thanks.

    Henry

  7. #6
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    238
    using eval should suggest you're doing it wrong.
    you should always try to find another way. if you don't find one, try harder.

    are your files always three lines?
    Code:
    awk 'BEGIN{ Y[1]=95; Y[2]=135; Y[3]=138 }{$1=$1; print Y[NR],$0}' OFS=',' file

  8. #7
    Just Joined!
    Join Date
    Aug 2012
    Posts
    24
    Dear guru,

    Thanks for your quick response. It works.

    Not always. It might extend to N lines in the future. It's just temporarily three lines now.

    Cheers,
    Henry

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •