Output Row if Third Column is Larger


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Output Row if Third Column is Larger
# 1  
Old 05-12-2013
Output Row if Third Column is Larger

Hi,

I am fairly new to Unix scripting. We are running Solaris 5.10.

I have the following question: Assume a text file with one text
column, followed by 2 integer columns.

How would I generate a script or, preferably, a command that will output
the rows in which the value of the third columns is greater than
the value of the second column.

For example, assume that file is named File1 and it
consists of the following.

Code:
C1  50 43
C2 100 65
C3  45  60
C4  10 5
C5   50 55

The output of the command or script should be:

Code:
C3 45 60
C5 50 55

Thanks,
QZ1
# 2  
Old 05-12-2013
Simplest would be:
Code:
awk '$3>$2' File1

# 3  
Old 05-12-2013
Quote:
Originally Posted by Scott
Simplest would be:
Code:
awk '$3>$2' File1

Even for something this simple, I would still suggest /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of the default awk on a Solaris 5.10 system.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 05-12-2013
Don, are you suggesting OP to use xpg4/xpg6 awk instead of awk because this code will fail or you want OP to avoid using it because it is broken and not recommended?

---------- Post updated at 18:05 ---------- Previous update was at 17:41 ----------

Nevermind, I understood.
# 5  
Old 05-12-2013
Quote:
Originally Posted by Yoda
Don, are you suggesting OP to use xpg4/xpg6 awk instead of awk because this code will fail or you want OP to avoid using it because it is broken and not recommended?
The awk in /bin or /usr/bin on Solaris 10 is the awk that was present in 1977-1985 UNIX Systems. On most UNIX and UNIX-like systems, nawk replaced awk in 1985 or shortly thereafter. On UNIX System V Systems (and on Solaris/SunOS 5.1 and later systems) the old awk kept the name awk and the new awk was named nawk.

If you want an historical version of awk to run awk scripts from that era use the Solaris 10 default awk. (On most other systems that have an awk from this era, it is known as oawk.) Otherwise, (including anytime your are writing new awk scripts to run on a Solaris 10 system) I believe you should use nawk, /usr/xpg4/bin/awk, or /usr/xpg6/bin/awk to get a version of awk that conforms to the POSIX and UNIX Standards and includes the updates to awk that Aho, Weinberg, and Kernighan described in their 1988 book The AWK Programming Language (ISBN 020107981X).

When writing new scripts, there is no reason to try to figure out if a script will be processed by the 1975 language interpreter in the same way that a current awk would interpret the script when we know that nawk and /usr/xpg[46]/bin/awk will reliably do what we all expect awk to do today.

PS. I am not suggesting that the default awk on Solaris systems is broken. I am suggesting that it is there to run historical awk scripts (which it does well). But, it should not be used when writing new awk scripts.

Last edited by Don Cragun; 05-12-2013 at 09:01 PM..
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 05-12-2013
Quote:
Originally Posted by Scott
Simplest would be:
Code:
awk '$3>$2' File1

Quote:
Originally Posted by Don Cragun
Even for something this simple, I would still suggest /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of the default awk on a Solaris 5.10 system.
My paranoid nature would go further and explictly cast one of the operands with +0.

As far as I know, most awk implementations (if not all of them) will treat $3>$2 as a numeric comparison, but POSIX requires a locale-dependent string comparison.

Excerpts from POSIX awk:
Quote:
Each field variable shall have a string value or an uninitialized value when created.

...

A string value shall be considered a numeric string if it comes from one of the following:

1. Field variables

...

Comparisons (with the '<', "<=", "!=", "==", '>', and ">=" operators) shall be made numerically if both operands are numeric, if one is numeric and the other has a string value that is a numeric string, or if one is numeric and the other has the uninitialized value. Otherwise, operands shall be converted to strings as required and a string comparison shall be made using the locale-specific collation sequence.
$2 and $3 are both strings. If their string values are valid numbers, they are numeric strings. But, even if both are numeric strings, neither is a numeric. According to POSIX, for a numeric comparison at least one of the operands must be a numeric.

Even if no extant implementation adheres to this, an explicit cast is good future-proofing.

Regards,
Alister

Last edited by alister; 05-12-2013 at 11:11 PM..
# 7  
Old 06-20-2013
Thanks

Hi,

Thanks to those who replied.

QZ1
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Split larger files into smaller ones with Column names

Hi, I have one large files of 100000 rows with header column. Eg: Emp Code, Emp Name 101,xxx 102,YYY 103,zzz ... ... I want to split the files into smaller files with only 30000 rows each..File 1,2 and 3 must have 30000 rows and file 4 must contain 10000 rows. But the column... (1 Reply)
Discussion started by: Nivas
1 Replies

2. Shell Programming and Scripting

Find larger on replicates and output

Hi All I have a question. I have a file like this: 10 name1 ID1 value1 value2 valueN.. 31 name2 ID1 value1 value2 valueN.. 20 name3 ID2 value1 value2 valueN.. 23 name4 ID2 value1 value2 valueN.. 33 name5 ID2 value1 value2 valueN.. 45 name6 ID2 value1 value2 valueN.. well, my output... (2 Replies)
Discussion started by: giuliangiuseppe
2 Replies

3. Shell Programming and Scripting

Print row on 4th column to all row

Dear All, I have input : SEG901 5173 9005 5740 SEG902 5227 5284 SEG903 5284 5346 SEG904 5346 9010 SEG905 5400 5456 SEG906 5456 5511 SEG907 5511 9011 SEG908 5572 9015 SEG909 5622 9020 SEG910 5678 5739 SEG911 5739 5796 SEG912 5796 9025 ... (3 Replies)
Discussion started by: attila
3 Replies

4. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

5. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

6. Shell Programming and Scripting

Column to row output

Hello All, i have a file with the contents like shown below Name = POLICY1 Selected = 1 Written = 0 Name = POLICY2 Selected = 6 Written = 8 Name = POLICY3 Selected = 4 Written = 26 I want the output to be... (5 Replies)
Discussion started by: vinredmac
5 Replies

7. Shell Programming and Scripting

convert a column to row output?

Getting tired of cut-and-paste...so I thought I would post a question. how do I change this column output to a single row? from this: # vgdisplay -v /dev/vgeva05 | grep dsk | awk '{print $3}' /dev/dsk/c6t0d5 /dev/dsk/c11t0d5 /dev/dsk/c15t0d5 /dev/dsk/c18t0d5 /dev/dsk/c7t0d5... (8 Replies)
Discussion started by: mr_manny
8 Replies

8. Shell Programming and Scripting

Moving data from a specified column/row to another column/row

Hello, I have an input file like the following: 11_3_4 2_1_35 3_15__ _16989 Where '_' is a space. The data is in a table. Is there a way for the program to prompt the user for x1,y1 and x2,y2, where x1,y1 is the desired number (for example x=6 y=4 is a value of 4) and move to a desired spot... (2 Replies)
Discussion started by: jl487
2 Replies

9. Shell Programming and Scripting

top output for six processes with the same name, output changed from column to row

Hi, I have a system under test, and I use a script that does a ps. The output, is in the following format, it's basically the timestamp, followed by the rss and vsize. 09:03:57 68404 183656 68312 181944 69860 217360 67536 182564 69072 183172 69032 199276 09:04:27 68752 183292 70000 189020... (5 Replies)
Discussion started by: Bloke
5 Replies

10. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question