Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk and multiple text tests for a field 1

Status
Not open for further replies.

stfaprc

Programmer
Feb 10, 2005
216
US
I want to print a line only under the condition that a certain field is not empty and does not equal the number 29 or 75.

if ( x !~ /[](29)(75)/ ) print x

is evidently not the way to do it. Anyone know how? thanks.
 
if(x!="" && x!=29 && x!=75)print

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Yes, that did work.
I was hoping there was a way to do it in one swoop with regular expressions instead of 3 seperate conditionals.
 
Perhaps this ?
if(x!~/^( *|29|75)$/)print

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Something like this?
Code:
[b]cat testfile.txt[/b]
45:24:11:98:73:134:21:3490:377
65::72:88:43:20:30:4400:244  <-- $2=empty
33:56:52:665:95:332:9888:21
92:89:45:27:71:576:2345:43
234:29:3:21:66:36:6766:433:3 <-- $2=29
455:23:45:77:77:65:56:45:333
2:75:24:7776:456:3::67:36    <-- $2=75

[b]awk -F":" '$2 !~ /(^$|29|75)/ { print $0 }' testfile.txt[/b]

45:24:11:98:73:134:21:3490:377
33:56:52:665:95:332:9888:21
92:89:45:27:71:576:2345:43
455:23:45:77:77:65:56:45:333
:)

 
geirendre, what happens with your awk program when $2=290 or 375 ?
 
OK, I'll be the first to admit that I'm no expert....
How about this:
Code:
awk -F":" '$2 !~ /(^$|^29$|^75$)/ { print $0 }' testfile.txt
then???
Or am I chasing the wrong horse?
 
A shorter way:
Code:
awk -F: '$2!~/^( *|29|75)$/' testfile.txt

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top