The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
replacing numbers greater than 0 with 1 vrms Shell Programming and Scripting 4 06-04-2008 07:40 AM
greater than less than in case sars Shell Programming and Scripting 2 02-18-2007 12:37 PM
AWK greater than 200 insania Shell Programming and Scripting 2 02-05-2007 03:03 PM
length of data greater than 11 thanuman UNIX for Dummies Questions & Answers 2 09-10-2004 07:13 AM
Greater Than Lem2003 Shell Programming and Scripting 2 08-29-2003 10:06 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-08-2008
dlam dlam is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 35
AWK greater than?

Sorry for such a basic question, but I have spent hours trying to work this out! I need an awk command (or similar) that will look at a text file and output to the screen if the 4th column of each line has a value greater than or equal to x.

data.txt
This is the 1 line
This is the 2 line
This is the 3 line

So if x was 2 I would expect it to output

This is the 2 line
This is the 3 line

I've found a number of awk command on the net that let you output if the string length is a certain size, but I can't find anything on how to check if a field has a certain value. Does such a command exist? (I'm using Solaris by the way)

Thanks for any help.
  #2 (permalink)  
Old 03-08-2008
naree naree is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 81
Answer

Try this probably complicated but works

for i in `cat data.txt | awk '{print $4}'`
> do
> if [ $i -ge 2 ]
> then
> cat data.txt | grep "$i"
> fi
> done

U will get Output:

This is the 2 line
This is the 3 line
  #3 (permalink)  
Old 03-08-2008
dlam dlam is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 35
Thanks for that it works a treat - typical of Unix to need such a long winded script for such a simple task!

Edit:- Just found an easier way of doing it. Weird that I searched this forum and didn't find a way, but once I have posted the question it appears in the "Similar Threads" bit down the bottom.

Anyhoo, this works a treat too (and is a lot shorter so I'll apologise to Unix for calling it long winded!): awk ' $4 > 2 ' data.txt
  #4 (permalink)  
Old 03-08-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,420
Code:
awk ' $4 >= 2 ' data.txt
  #5 (permalink)  
Old 03-08-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,913
Quote:
typical of Unix to need such a long winded script for such a simple task!
Not at all true if you know what you are doing in UNIX.

Here is a one liner which does what you want.
Code:
awk -v x=2 '$4 >= x'  data.txt
Just set x to any value you want via the "-v x=" option.
  #6 (permalink)  
Old 03-09-2008
dlam dlam is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 35
It's OK, I've already apologised further up the thread for saying it was long winded.

Thanks for all the help guys.
Closed Thread

Bookmarks

Tags
solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:50 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0