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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pattern Matching

Status
Not open for further replies.

hawkeye71

Technical User
Feb 9, 2001
45
US
Hi Folks:

I am trying to print a line if the pattern is not on that line.
I am writing all the statements in a script file.
I can make it work from the command line, but it did not work when i try to do the same from the script file.

For example, my input file is:
Hello World
Hello USA[1]
Hello USA[2] USA[3]

I only need to print when a line does not contain USA.
The following line works from command line:
awk '$2 !~/USA/ && $3 !~/USA/ {print $0}' <input file>

But when i put the same in the script file, it does not give the desired results. I have even tried to use the if condition, but the result stays the same.

In summary, I want to print a line if it does not contain a certain pattern.

Thanks,
Indiana
 
nawk -f usa.awk myFile.txt

#------------------ usa.awk
!/USA/

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
VGersh99:

Do you want me to type
#include<disclaimer.h> in my script file.

In addition, I do not have access to nawk. When I type the &quot;nawk&quot; on command line, the response is &quot;nawk: Command not found.&quot;.

Thanks for help.

Thanks,
Indiana
 
No, PLS don't include my signature - it's not meant to be awk-proof ;)
What OS are you on?

awk -f usa.awk myFile.txt

#------------------ usa.awk
!/USA/
 
I am using HP-UNIX 11

The following worked for me:

{
if ($2 !~ /USA/ && NF != 0) {
if ($3 !~ /USA/) {
print $0}
}
}



Thanks,
Indiana
 
why don't use 'grep -v USA yourfile'? few keystrokes and more efficiency.
 
because it is an AWK forum? ;)

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top