1

I have this

    -77.199997 23.6343749630 39.9989584580 
    -80.000000 23.6364583430 39.9989584580
    -81.599998 23.6385417220 39.9989584580

I want to reorder the columns so it reads column 2, column 3, column 1 (lat,long,elevation)

I have used awk many times before but for some reason when reordering the columns, it comes out like this, with an extra space

     32.1614584410 43.3385414490
     -2019.400024 
     32.1635418210 43.3385414490
     -2016.800049 
     32.1656252000 43.3385414490
     -2018.500000 

I have many more but could someone tell me as to why its doing this or am I missing something

    awk -F" " '{print " "$2" "$3" "$1" "}' infile.xyz > outfile.xyz
    awk '{print $2 $3 $1}' infile.xyz > outfile.xyz
    awk -F" " '{print ""$2" "$3" "$1""}' infile.xyz > outfile.xyz
3
  • 2
    awk '{print $2,$3,$1}' infile...
    – jasonwryan
    Feb 2 '16 at 23:28
  • It could be that infile.xyz is carriage return terminated. Run dos2unix on it first
    – iruvar
    Feb 3 '16 at 0:22
  • Sweet, dos2unix worked. Never had to do that before
    – lbeazy
    Feb 3 '16 at 15:48
2

I had to run dos2unix on all the infiles and everything worked fine after that.

1
  • +1, I, too, had the same problem due to Windows style line endings. Piping the input and adding perl -p -e 's/\r$//' worked for me (which essentially like running dos2unix on infiles)
    – morfizm
    Sep 29 '17 at 22:03
1

First, make sure you are using spaces and not tabs in the input file.

Then, try separating the variables by commas, like this:

awk '{print $2,$3,$1}' infile.xyz > output.xyz

If you place the variables side by side, like in $2 $3 $1, awk concatenates them.

Also, you may want to try the printf function, as it provides better formatting capabilities.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.