awk Help: quick and easy question may be: How to use &&


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk Help: quick and easy question may be: How to use &&
# 1  
Old 01-28-2013
awk Help: quick and easy question may be: How to use &&

Hi Guru's.

I am trying to use to check if $5 is greater than 80 & if not 100, then to print $0 :

Code:
awk '{ if ($5>80) && if ($5 != 100) print $0}




But getting error:

Code:
>bdf1|sed 's/%//g'|awk '{ if ($5>80) && if ($5 != 100) print $0}'
 syntax error The source line is 1.
 The error context is
                { if ($5>80) >>>  && <<< 
 awk: The statement cannot be correctly parsed.
 The source line is 1.


Please advise how to use two condition within awk,
Thanks a lot,
# 2  
Old 01-28-2013
Code:
[root@host ~]# echo "a a a a 80 a" | awk '( $5 > 80 ) && ( $5 != 100 ) { print $0 }'
[root@host ~]# echo "a a a a 99 a" | awk '( $5 > 80 ) && ( $5 != 100 ) { print $0 }'
a a a a 99 a
[root@host ~]# echo "a a a a 100 a" | awk '( $5 > 80 ) && ( $5 != 100 ) { print $0 }'
[root@host ~]# echo "a a a a 101 a" | awk '( $5 > 80 ) && ( $5 != 100 ) { print $0 }'
a a a a 101 a

# 3  
Old 01-28-2013
Also
Code:
awk '$5>80&&$5!=100{print $0}' filename

OR
Code:
awk '{if(($5>80)&&($5!=100)) print $0 } ' filename


Last edited by Yoda; 01-28-2013 at 12:53 PM..
This User Gave Thanks to Yoda For This Post:
# 4  
Old 01-28-2013
Indeed - I include the parenthesis and spaces for readability, though due to rules of precedence they aren't required.
# 5  
Old 01-28-2013
Thanks, ( zazzybob && bipinajith ) , great answers, I like both, however the short one from bipinajith is great.
# 6  
Old 01-29-2013
Code:
awk '$5>80&&$5!=100' filename

As you want to print the entire line, we dont need to use print $0
# 7  
Old 01-29-2013
I think it's best to be a little more verbose when specifying commands like this - especially within scripts. A year later when you come back to look at this (especially since you are asking for help in the first place), you might not understand what:

Code:
awk '$5>80&&$5!=100' filename

is doing, whereas

Code:
awk '( $5 > 80 ) && ( $5 != 100 ) { print $0 }' filename

is more readable.

Of course - all solutions are technically correct, and this is just personal opinion - but after 20 years of looking at others' shell scripts, readability is worth a few extra key strokes. On the command line, not so much.

Cheers,
ZB
These 2 Users Gave Thanks to zazzybob For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SFTP Shell Script Get & Delete && Upload & Delete

Hi All, Do you have any sample script, - auto get file from SFTP remote server and delete file in remove server after downloaded. - only download specify filename - auto upload file from local to SFTP remote server and delete local folder file after uploaded - only upload specify filename ... (3 Replies)
Discussion started by: weesiong
3 Replies

2. Shell Programming and Scripting

awk question: How to print condition of NR & NF together.

Experts: LINE1 :This is line one The FIRST line of the file. LINE2 :This is line two LINE3 :This is line three with 8 fileds LINE4 :This is line four LINE5 :This is line five LINE6 :This is line six with 8 fileds I want to delete line 1, and then process the file and want to print lines... (2 Replies)
Discussion started by: rveri
2 Replies

3. Shell Programming and Scripting

README: Factorial quick chart with sed & bc:

Hi all, While doing some checks I found a kind of interesting arithmetic factorial chart with sed, sharing this may be simple but thought to share, # n=20;for i in `seq $n`;do printf "`seq $i|xargs|sed 's/ /*/g'`= ";echo "`seq $i|xargs|sed 's/ /*/g'`"| bc;done 1= 1 1*2= 2 1*2*3= 6... (6 Replies)
Discussion started by: rveri
6 Replies

4. Programming

Quick question about '_&'

I've seen in other programmers code the use of '_&' as a line separator. I am trying to find in my C++ reference manual some pages dedicated to an explanation of the use of this '_&' but I don't know what it is called. I only know it is a "line separator" or "line break" of some sort which is... (0 Replies)
Discussion started by: sepoto
0 Replies

5. Shell Programming and Scripting

Replace & sign to &amp word

Hi, I have text file abc.txt. In this file, I have the following data. Input: Mr Smith &amp Mrs Smith Mr Smith &apos Mrs Smith Mr Smith & Mrs Smith Mr Smith& Mrs Smith Mr Smith &Mrs Smith Output: Mr Smith &amp Mrs Smith Mr Smith &apos Mrs Smith Mr Smith &amp Mrs Smith Mr Smith&amp... (4 Replies)
Discussion started by: naveed
4 Replies

6. Shell Programming and Scripting

replace & with &amp; xml file

Hello All I have a xml file with many sets of records like this <mytag>mydata</mytag> <tag2>data&</tag2> also same file can be like this <mytag>mydata</mytag> <tag2>data&</tag2> <tag3>data2&amp;data3</tag3> Now i can grep & and replace with &amp; for whole file but it will replace all... (4 Replies)
Discussion started by: lokaish23
4 Replies

7. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

8. Shell Programming and Scripting

perl newbie . &&..programming newbie (question 2)

Hello everyone, I am having to do a lot of perl scripting these days and I am learning a lot. I have this problem I want to move files from a folder and all its sub folders to one parent folder, they are all .gz files.. there is folder1\folder2\*.gz and there are about 50 folders... (1 Reply)
Discussion started by: xytiz
1 Replies

9. Shell Programming and Scripting

Notepad help needed easy quick question

TAKE A LOOK AT THE ATTACHED PICTURE. my goal is just to SELECT n DELETE Those lines : Dialogue: Marked=0,0:02:39.49,0:02:40.49,Default,NTP,0000,00 00,0000,!Effect, (without the text) take a look at that picture... the marked line... i just need ALL of these lines removed.. i managed... (3 Replies)
Discussion started by: mr_spidey
3 Replies

10. Shell Programming and Scripting

Awk: Version && nextfile

How can I find which version of Awk is installed? OpSystem is HPUX 11.x I am getting an error when trying to use the keyword nextfile and I dont know why! (Well, I can only assume that I have am using a version of Awk that does not support nextfile. However, according to O'Reilly, nextfile is... (3 Replies)
Discussion started by: google
3 Replies
Login or Register to Ask a Question