Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems using awk to make many transformations in a text file

Status
Not open for further replies.

Geomatica

Technical User
Jan 10, 2012
4
CA
Hi, I'm new with awk and have some trouble with it. I have a file with geographic coordinates like this:

49.397506N,123.505509W,-2.63
49.397509N,123.505495W,-2.58
49.397503N,123.505481W,-2.54
49.397505N,123.505468W,-2.50
49.397504N,123.505454W,-2.48
49.397502N,123.505440W,-2.44

The three columns (separated by commas) represent Y, X and Z values. I want it to be X, Y and Z, to remove the letters, and to make the X negative and the Z positive.

I run this code in cmd:

awk –F[,NW] -v OFS=, {print-$3,$1,-$5} originalfile.txt > newfile.txt

The new file is like this:

-123.506,49.397506,2.63
-123.505,49.397509,2.58
-123.505,49.397503,2.54
-123.505,49.397505,2.5
-123.505,49.397504,2.48
-123.505,49.397502,2.44

It is perfect except that it removes three digits from my X.
I tried many different codes and searched on forums, but I could not find anything to help me.

I hope you will help me! Thanks a lot!
 


Try:
Code:
awk -F[,NW] -v OFS=, '{printf "%f,%f,%3.2g\n",-$3,$1,-$5}' originalfile.txt > newfile.txt
[3eyes]



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Thanks a lot for replying!

I don't understand why, it gives me a syntax error for each % signs and for \ (backslash not last character on line).
 
Hi

Alternatively you can just use a string minus instead of the unary operator :
Code:
awk -F'[,NW]' -v OFS=, '[teal]{[/teal][COLOR=chocolate]print[/color][green][i][highlight]"[/highlight]-[highlight]"[/highlight][/i][/green][navy]$3[/navy][teal],[/teal][navy]$1[/navy][teal],-[/teal][navy]$5[/navy][teal]}[/teal]
' originalfile.txt > newfile.txt
Note that if you are on Windows then you may have to play with the escaping of the quotes.

Tested with [tt]gawk[/tt] and [tt]mawk[/tt].


Feherke.
 

Show us your code, use the "code, /code" tags in square brackets to format your code.

Also, when I copied and pasted your original command into a text file, it appeared with invalid characters (did you copy/paste from Word or Wordpad?) -- if so DON'T!
[noevil]



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Thanks for your answers, but even with them I'm not able to keep all my digits in the output file.

I write my code in the command prompt (cdm) on a computer using Windows XP. My code is:

Code:
cd desktop\GnuWin32\bin
awk -F[,NW] -v OFS=, {print-$3,$1,-$5} originalfile.txt > newfile.txt

I'm sorry, I'm not that good in programming. My specialty is GIS and webmapping. :p

Thanks again.
 
To save having to debug Windows weird handling of quotes, I recommend you create a separate file containing the awk code, e.g. mycode.awk:

Code:
[green]BEGIN[/green] {
    [blue]FS[/blue]=[red]"[/red][purple][,NW][/purple][red]"[/red]
    [blue]OFS[/blue]=[red]"[/red][purple],[/purple][red]"[/red]
}
{ [b]print[/b][red]"[/red][purple]-[/purple][red]"[/red][blue]$3[/blue],[blue]$1[/blue],-[blue]$5[/blue] }

And then use:

Code:
awk -f mycode.awk originalfile.txt > newfile.txt

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Thanks a lot Annihilannic!

This solve the problem!

Thanks to all for your help!
 
Hi

Another workaround would be to use CygWin instead of GnuWin32. There would be no quoting problems with its Bash.


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top