Manipulating xyz file with awk-if-else or sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Manipulating xyz file with awk-if-else or sed
# 1  
Old 07-11-2012
Manipulating xyz file with awk-if-else or sed

Hi forum, I am really hoping somebody can please help me here.

I have a dataset in xyz format, with longitude as x, latitude as y and data readings as z.

eg.

Code:
0 90 -8
1 90 23
2 90 -4
etc etc etc

What i am looking to do is format the data so that x and y are untouched, however in z:

if z > 0 then ignore, else if z < 0 then add 20 to the z value

If anyone knows a better way to accomplish this than an awk command then feel free to contribute. I am very new to unix and am still learning the ropes.

I know the problem most people have in these posts is not conveying their question properly, so i hope mine is understood clearly.

Many thanks in advance, Sam.

Last edited by joeyg; 07-11-2012 at 01:43 PM.. Reason: Please wrap data and sripts with CodeTags
# 2  
Old 07-11-2012
You don't need if-else for this. The 'ignore' happens by default anyway, you don't need a case for that.

You can put a condition in front of a code block which will cause it to be executed only when it's true, sort of like an if. Also, you can do math on columns directly.

Code:
awk '($3+0) < 0 { $3 += 20 } 1' filename

The ($3+0) is just to turn the third column, $3, from a string into a number so < > = will work on it as expected.

Last edited by Corona688; 07-11-2012 at 01:59 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 07-12-2012
Thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Manipulating xml data with awk

Hi everyone, I have a little bit of complicated task to finish with AWK. Here it is; I have a data file in xml format which looks like this <data> a1 a2 a3 a4 a5 b1 b2 b3 b4 b5 c1 c2 c3 c4 c5 d1 d2 d3 d4 d5 e1 e2 e3 e4 e5 </data> lets say each data block contains 5 rows and 5 columns,... (13 Replies)
Discussion started by: hayreter
13 Replies

2. Shell Programming and Scripting

Manipulating a variable using sed (solved)

Hi, My variable has value as this: tvar1="bool_risk_enabled" Boolean "true" Now I need to replace this true with false. Which is the best way to do this? Can we do this with sed command? Please help me. ---------- Post updated at 05:23 PM ---------- Previous update was at 05:00 PM... (2 Replies)
Discussion started by: pravintse
2 Replies

3. Shell Programming and Scripting

awk xyz file in a for loop

hi good day I have a number of text files (xyz) that I want to format in to defined column widths (that is 7 digits (padding with zeros in cases where necessary) with 6 decimal places) using awk. . I can perform this on one text file at a time without any problems. However I want to write a... (2 Replies)
Discussion started by: johnstrong
2 Replies

4. Shell Programming and Scripting

Manipulating a header file using awk or sed

Hi Guys, Is there a simple way of doing the below. Available<spaces>Assigned<spaces>Maximum<spaces>Maximum<spaces>Page<spaces>Total <spaces>Used<spaces>Pct<spaces>Max. Pct<CR> Space<spaces>Capacity<spaces>Extension<spaces>Reduction<spaces>Size<spaces>... (8 Replies)
Discussion started by: eo29
8 Replies

5. Shell Programming and Scripting

Manipulating the etc/passwd file with sed

How can i use sed to extract the user name and home directory from the /etc/passwd/ file on my server. (11 Replies)
Discussion started by: Pauline mugisha
11 Replies

6. Shell Programming and Scripting

Manipulating Pick multi dimensional data with awk.

Hi. I am reasonably new to awk, but have done quite a lot of unix scripting in the past. I have resolved the issues below with unix scripting but it runs like a dog. Moved to awk for speed and functionality but running up a big learning curve in a hurry, so hope there is some help here. I... (6 Replies)
Discussion started by: mike.strategis
6 Replies

7. Shell Programming and Scripting

manipulating Fields in file using SED

Hi, I have two issues: I have one file say file1.dat and its over 3GB. It contains pipe delimited fields. The first line in the file is the header field which tells the column names etc. and from second line it's the data fileds with pipe delimited. Something like below: ... (5 Replies)
Discussion started by: rkumar28
5 Replies

8. UNIX for Dummies Questions & Answers

Help!! manipulating file

Hi all, I need help manipulating the file below. Here is what I needed to do. First, I have to replace INSUPD to DELETE. Then I need to change the content of the file around by flipping the contents in the file from the bottom to the top (start from "CMD") How should I attack this? Here... (2 Replies)
Discussion started by: sirrtuan
2 Replies

9. Shell Programming and Scripting

Manipulating awk $variables using sed?

I have been searching around the forums here trying to find a solution to my problem but not getting anywhere but closer to baldness. I have a 20 column pipe "|" seperated text file. The 14th variable doesnt always exist, but will have the format of YYYYMM or YYYY if it does. I need to take... (2 Replies)
Discussion started by: r0sc0
2 Replies

10. UNIX for Dummies Questions & Answers

How would you replace the n character in a file with some xyz?

How would you replace the n character in a file with some xyz? (2 Replies)
Discussion started by: JosephGerard
2 Replies
Login or Register to Ask a Question