awk , conditional involving line and column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk , conditional involving line and column
# 8  
Old 02-26-2012
@agama: thank a lot, sorry for making confusing.
surely what you gave is what I want.
may I ask a more explanation about this part " $2+0 > simax "
why we need to write +0 , what does it means?
# 9  
Old 02-26-2012
Quote:
Originally Posted by ariesto
@agama: thank a lot, sorry for making confusing.
surely what you gave is what I want.
may I ask a more explanation about this part " $2+0 > simax "
why we need to write +0 , what does it means?
No problems; I find myself often missing little things and assuming incorrectly, so it can work both ways!

The $2+0 forces the value to be interpreted/stored as a number rather than a string. It's a habit that I've gotten into (not sure why I didn't do it on the value assignment) which prevents odd issues when comparing values. This practice stems from my experience with very early versions of awk; awk currently distributed with Solaris (not nawk) still exhibits the need for this in certain circumstances.

Consider a data file with colons used as field separators and the input line:
11 : 2

And the small awk programme:
Code:
awk -F : '{ if( $1 > $2 ) print "true" }' input-file

With most (all?) modern awk implementations, running the programme results in 'true' being written to stdout. However, older awks interpret the fields as strings because of the white space, and '11' in field 1 does not evaluate larger than '2' as might be expected. To prevent this, adding zero to the variable forces awk to convert it to a numeric value and there aren't any surprises. The programme
Code:
awk -F : '{ if( $1+0 > $2+0 ) print "true" }' input-file

works as expected when executed with an older version of awk; even when the input data has whitespace.

This practice probably isn't needed with modern implementations of awk, and even with older versions the conditions must be 'just right' to trigger the need. However, I've been bitten enough times (and spent countless hours tracking down the cause of the odd logic issues this causes) to error on the side of adding zero to variables pulled from the input fields like I did in the programme I posted.
# 10  
Old 02-26-2012
Good explanation Agama!Smilie
This User Gave Thanks to pandeesh For This Post:
# 11  
Old 02-28-2012
sorry asking (probably) silly question. "simax" this is general term for maximum value of everything or because in my question, for maximum value of Si type of atom, so other words can be used?

---------- Post updated at 07:35 PM ---------- Previous update was at 07:20 PM ----------

another question how if type of atoms also indicated by number
type z vz
2 34 54
1 20 56
3 14 13
2 40 17
1 65 18
3 70 19
2 24 20
3 85 21
1 90 12
2 12 34
so this command : if( $1 == "Si" && $2+0 > simax ) should be changed to
if( $1 == "2" && $2+0 > simax ) or
if( $1 == 2 && $2+0 > simax )?
thank you very much
# 12  
Old 02-28-2012
Quote:
Originally Posted by ariesto
sorry asking (probably) silly question. "simax" this is general term for maximum value of everything or because in my question, for maximum value of Si type of atom, so other words can be used?
I chose simax as it represented the maximum value observed for Si.

Quote:
another question how if type of atoms also indicated by number
type z vz
2 34 54
1 20 56
3 14 13
2 40 17
1 65 18
3 70 19
2 24 20
3 85 21
1 90 12
2 12 34
so this command : if( $1 == "Si" && $2+0 > simax ) should be changed to
if( $1 == "2" && $2+0 > simax ) or
if( $1 == 2 && $2+0 > simax )?
thank you very much
Best to use the later. I would code
[code]
if( $1+0 == 2 && $2+0 > simax)
[icode]

Adding zero to the value forces awk to treat it as a numeric rather than a string. Probably not significant in your case, but a habit of mine to prevent surprises with old versions of awk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conditional Column Value

Hi Folks, I'm trying tog ain further experience with shell programming and have set my a small goal of writing a little filesystem monitoring script. So far my output is as follows: PACMYDB03 Filesystem Size Used Avail Use% Status /usr/local/mysql/data ... (5 Replies)
Discussion started by: Axleuk
5 Replies

2. Shell Programming and Scripting

Filter on one column and then perform conditional calculations on another column with a Linux script

Hi, I have a file (stats.txt) with columns like in the example below. Destination IP address, timestamp, TCP packet sequence number and packet length. destIP time seqNo packetLength 1.2.3.4 0.01 123 500 1.2.3.5 0.03 44 1500 1.3.2.5 0.08 44 1500 1.2.3.4 0.44... (12 Replies)
Discussion started by: Zooma
12 Replies

3. UNIX for Dummies Questions & Answers

awk help: how to pull phrase and one column from line above?

Hi everyone, Here's my awk statement so far: awk '/TOTAL TYPE:/{print x;print};{x=$0}' file1 >file2 'file1' has too much proprietary data in it to include here, so let's go with the output from code above. It looks like this: 123456 JAMES T KIRK D ... (2 Replies)
Discussion started by: Scottie1954
2 Replies

4. Shell Programming and Scripting

Awk next line as column

Hi, This forum rocks. I think this might be an easy thing, but since I am new to awk, please help me. input: x y z 1 a b c 2 d e f 3 g h i 7 output: x y z 1 a b c 2 d e f 3 (8 Replies)
Discussion started by: jacobs.smith
8 Replies

5. Shell Programming and Scripting

Counting rows line by line from a specific column using Awk

Dear UNIX community, I would like to to count characters from a specific row and have them displayed line-by-line. I have a file called testAwk2.csv which contain the following data: rabbit penguin goat giraffe emu ostrich I would like to count in the middle row individually... (4 Replies)
Discussion started by: vnayak
4 Replies

6. Shell Programming and Scripting

awk search column, print line

Hello. I've been banging my head against walls trying to search a comma delimited file, using awk. I'm trying to search a "column" for a specific parameter, if it matches, then I'd like to print the whole line. I've read in multiple texts: awk -F, '{ if ($4 == "string") print $0 }'... (2 Replies)
Discussion started by: Matthias03
2 Replies

7. Shell Programming and Scripting

Replace a column with a value conditional on a value in col1

Hi, Perhaps a rather simple problem...? I have data that looks like this. BPC0013 ANNUL_49610 0 0 1 1 BPC0014 ANNUL_49642 0 0 2 1 BPC0015 ANNUL_49580 0 0 1 1 BPC0016 ANNUL_49596 0 0 2 1 BPC0017 VULGO_49612 0 0 1 1 BPC0018 ANNUL_49628 0 0 1 1 BPC0019 ANNUL_49692 0 0 2 1 170291_HMG... (4 Replies)
Discussion started by: genehunter
4 Replies

8. Shell Programming and Scripting

Conditional aggregation and print of a column in file

Hi My input file looks like field1 field2 field3 field4 field5 field1 field2 field3 field4 field5 field1 field2 field3 field4 field5 :::::::::::: :::::::::::: There may be one space of multiple spaces between fields and no fields contains spaces in them. If field 1 to 4 are equal for... (3 Replies)
Discussion started by: bittoo
3 Replies

9. Shell Programming and Scripting

awk convert from line to column

i have an output like this : 012008 25760883 022008 12273095 032007 10103 032008 10115642 042007 20952798 but i would like to have it like this 012008,25760883 022008,12273095 032007,10103 032008,10115642 042007,20952798 (4 Replies)
Discussion started by: jarmouda
4 Replies

10. Shell Programming and Scripting

line to column using awk

hi, i'm a newbie and this is my first post here. 'hope all of you fellow members are doing fine. so here is my first thread to ask for help on how to use awk language to do this task. i have a file to process and after a series of other awk commands and shell scripts i managed to convert the... (11 Replies)
Discussion started by: genix2008
11 Replies
Login or Register to Ask a Question

Featured Tech Videos