AWK greater than?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK greater than?
# 1  
Old 03-08-2008
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  
Old 03-08-2008
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  
Old 03-08-2008
Thanks for that it works a treat - typical of Unix to need such a long winded script for such a simple task! Smilie

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  
Old 03-08-2008
Code:
awk ' $4 >= 2 ' data.txt

# 5  
Old 03-08-2008
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  
Old 03-09-2008
It's OK, I've already apologised further up the thread for saying it was long winded. Smilie

Thanks for all the help guys.
# 7  
Old 03-10-2008
More SImpler one!!!!!!!!!!!!!!!!!!!!!!

awk '$4>=2{print}' filename

Regards,
aajan
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk: greater than sign is working upside down

Hi, I noticed a weird behaviour with awk. input: A|B|1-100|blabla_35_40_blabla;blabla_53_60_blabla;blabla_90_110_blabla Objective: For each string separated by ';' in $4, if the first and second numbers are included in the interval in $3, then print "TRUE". Otherwise print "FALSE". In... (3 Replies)
Discussion started by: beca123456
3 Replies

2. Shell Programming and Scripting

Xargs, awk, match, if greater - as a one-liner

Hi I have multiple files for which I want to use awk for the following: Read each line in each file- if any of the columns match "PVALUE=" followed by the number, then print the line in case the number following "PVALUE=" is greater than 0.05. I did the following: ls *.txt | xargs -I @ -P15... (14 Replies)
Discussion started by: ts89490
14 Replies

3. Shell Programming and Scripting

Calculate percentage of columns greater than certain value in a matrix using awk

This matrix represents correlation values. Is it possible to calculate the percentage of columns (a1, a2, a3) that have a value >= |0.5| and report the percentage that has positive correlation >0.5 and negative correlation <-0.5 separately. thanx in advance! input name a1 a2 a3... (5 Replies)
Discussion started by: quincyjones
5 Replies

4. Shell Programming and Scripting

awk to get values greater than

data.txt August 09 17:16 2013 August 09 17:17 2013 August 09 17:19 2013 August 09 17:20 2013 August 09 17:21 2013 August 09 17:22 2013 August 09 17:23 2013 August 09 17:24 2013 to print from a point in this file, to the end of the file, i type: awk '/August 09 17:22/,0' data.txt. ... (1 Reply)
Discussion started by: SkySmart
1 Replies

5. Shell Programming and Scripting

AWK: Cannot read Number of records greater than 1(NR>1)

Hi all, I have a tab-delimited text file of size 10Mb. I am trying to count the number of lines using, grep -c . sample.txtor wc -l < sample.txt or awk 'END {print NR}' sample.txtAll these commands shows the count as 1, which means they are reading only the first header line of the file.... (3 Replies)
Discussion started by: mehar
3 Replies

6. Shell Programming and Scripting

Greping the column with a value greater than 50 with awk

Hi All, I am running a command from a remote server using ssh to different servers. I will get a output like below with 4 columns. I want to grab line which is having a coulmn which grate than or equal to 50. How can I do it with Awk or sed ??. I add a space to first row with sed 's/::/:: /g' to... (4 Replies)
Discussion started by: raghin
4 Replies

7. Shell Programming and Scripting

Need to Grep or awk a logfile for greater than value

Hello all Hoping someone would be kind enough to suggest a solution to a problem i have, and see if maybe i can even do this without a script. Essentially i have a very large log file, and within it each line had a value called TTMT, and it records a variable number in the following way, so... (6 Replies)
Discussion started by: 1905
6 Replies

8. Homework & Coursework Questions

Problem with awk,not able print the file that is greater than 3000 bytes.

My Script: #!/bin/sh date=`date +%y%m%d -d"1 day ago"` in_dir=/vis/logfiles/to_solmis cp `grep -il ST~856~ $inbound_dir/*$date*` /vis/sumit/in_ASN/ for i in /vis/sumit/in_ASN/* do mkdir -p /vis/sumit/inboundasns.$date cp `echo $i`... (1 Reply)
Discussion started by: shrima.pratima
1 Replies

9. Shell Programming and Scripting

AWK greater than 200

I need to grep out usernames with UID's greater than 200 out of the /etc/passwd file for a server migration. It is the third field however I am not sure to to accomplish this. I tried to search the forums but did not seem to find an answer (might have over looked it). Any help would be... (2 Replies)
Discussion started by: insania
2 Replies

10. Shell Programming and Scripting

[awk]: Row begins by random number and field 10 is greater than 10.00%

Hello! I wish to extract the pid where CPU is above 10% last pid: 22621; load averages: 4.71, 5.04, 5.13 15:08:34 221 processes: 212 sleeping, 2 running, 1 stopped, 6 on cpu CPU states: %... (3 Replies)
Discussion started by: Lomic
3 Replies
Login or Register to Ask a Question