Pattern not found : AWK , help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pattern not found : AWK , help
# 1  
Old 03-10-2008
Pattern not found : AWK , help

Hi All,

My file :

Code:
$ cat my.txt
AA:22:note:AA
BB:AA:help:XX
CC:14:AA:CC
ZZ:AA:hello:AA

A) <searching "AA" as 2nd field in all lines of my.txt >
$ awk -F ":" '{ if ($2 ~ /AA/) print "found in line - " NR; else print "Not found"}' my.txt
Not found
found in line - 2
Not found
found in line - 4

B) <"XX" is not present as 2nd field in my.txt>
$ awk -F ":" '{ if ($2 ~ /XX/) print "found in line - " NR; else print "Not found"}' my.txt
Not found
Not found
Not found
Not found

How can I output this:

- A) output should be:
Code:
found in line - 2
found in line - 4

** I can do a
grep -v "Not found" in the awk output to remove the "Not found"(s), but I want to do it with awk without doing "pipe"

- B) output should be:

Code:
Not found

** i.e. a single "Not found" if pattern is not present as 2nd field in any of the lines.

HTH,
jkl_jkl
# 2  
Old 03-10-2008
Should be something like:


Code:
awk -F ":" '$2 ~ /AA/ {print "found in line - " NR}' my.txt

and

Code:
awk -F ":" '$2 ~ /XX/{f=1} END{if(!f){print "Not found"}}' my.txt

Regards
# 3  
Old 03-12-2008
Franklin, thanks for your reply.

An additional query, how can I combine both the above logics together:

i.e.

if a particular pattern is found in 2nd field, then something like:

found in line - 2
found in line - 4

else

print that "pattern not found"

I tried this:

Code:
$ awk -F ":" '$2 ~ /XX/{f=1} END{if(!f){print "not found"} else {print "found in line : " NR}}' my.txt
not found

$ awk -F ":" '$2 ~ /AA/{f=1} END{if(!f){print "not found"} else {print "found in line : " NR}}' my.txt
found in line : 4

** I am missing found in line : 2 above

HTH,
jkl_jkl
# 4  
Old 03-12-2008
Code:
awk -F":" '$2~/AA/{print ..... }
$2 !~ /XX/ {print "..." } ' file

# 5  
Old 03-12-2008
I mean to say
if the a pattern, suppose "AA" is found as 2nd field then
print
found in line: 2
found in line: 4

and if "AA" is not found as 2nd field in any of the lines in the file, then
print
not found

HTH,
jkl_jkl
# 6  
Old 03-12-2008
Try this one:

Code:
awk -F ":" '$2 ~ /AA/ {print "found in line - " NR;next}{print "NOT found in line - " NR}' my.txt

EDIT: Oops, I misread your question, Radoulov's solution should work!

Regards

Last edited by Franklin52; 03-12-2008 at 08:39 AM.. Reason: misread question
# 7  
Old 03-12-2008
Code:
awk 'END { if (!x) print "not found" }
$2 == "AA" { print "found in line:", NR; x++ }
' FS=":" file

Use nawk or /usr/xpg4/bin/awk on Solaris.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed/awk join lines once pattern found

Hi all OS - RHEL6.4 I have input file -f1.txt I need to search line which starts with \Start and read next line till it gets blank line and join them all. I need to trim any trailing spaces for each line.So output.txt should be.. \Start\now\fine stepwatch this space for toolsends... (7 Replies)
Discussion started by: krsnadasa
7 Replies

2. Shell Programming and Scripting

Print character after pattern found

Hi Gurus, i need your help to create a script the will print a characters after the pattern was found. Sample lines are below: My birthday:"1977-16-07", My birthday:"1975-16-07" My birthday:"1970-16-07". My patter should be "birthday:", then i want to print the following characters which... (18 Replies)
Discussion started by: scripter123
18 Replies

3. Shell Programming and Scripting

If first pattern is found, look for second pattern. If second pattern not found, delete line

I had a spot of trouble coming up with a title, hopefully you'll understand once you read my problem... :) I have the output of an ldapsearch that looks like this: dn: cn=sam,ou=company,o=com uidNumber: 7174 gidNumber: 49563 homeDirectory: /home/sam loginshell: /bin/bash uid: sam... (2 Replies)
Discussion started by: samgoober
2 Replies

4. Shell Programming and Scripting

awk to print all lines after a pattern is found

Is there a way with aw to print all lines after a string is found There is a file like this ....... ........ 2012/19/11 :11.58 PM some data lne no date 2012/19/11 :11.59 PM some other data 2012/20/11 :12.00 AM some other data some line without dates some more lines without dates... (8 Replies)
Discussion started by: swayam123
8 Replies

5. Shell Programming and Scripting

awk with exit status if pattern not found

Hi everyone, Looking at writing another awk program here to find a pattern on a text file. If patterns aren't found I want the awk program to exit and then bash to file a failure log on the local machine. Then later a management framework we have in place will read if that failure log file... (3 Replies)
Discussion started by: Zaphod_B
3 Replies

6. Shell Programming and Scripting

To find the line no, where the particular pattern is not found

Hi, suppose i have a txt file containing thye following data 2012156|sb3|nwknjps|BAYONNE|NJ|tcg 201221|094|mtnnjprc:HACKENSACK|NJ|tcg 201222|wn3|mtnnjtc|HACKENSACK|NJ|tcg 2018164|ik4|mtnntc|JERSEY CITY|NJ|tcg 20123482|ik4|mtnnjpritc,JERSEY CITY|NJ|tcg... (3 Replies)
Discussion started by: priyanka3006
3 Replies

7. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

8. Shell Programming and Scripting

How to replace a line below where the pattern found

Hi All, I have a file say abc.xml. In this file, I need to search for a pattern “SAP_GATEWAY_HOST”; if this pattern found and the next line also contain the pattern “nwprc03.cos” then I need to replace this pattern “nwprc03.cos” with some other pattern “nwdrc03.apjp”. $ cat abc.xml... (3 Replies)
Discussion started by: Ritesh.patni84
3 Replies

9. Shell Programming and Scripting

Finding Last occurance of another pattern when a pattern is found.

Hi, I have two files viz, rak1: $ cat rak1 rak2: $ cat rak2 sdiff rak1 rak2 returns: I want the lines that got modified, changed, or deleted preceding with the section they are in. I have done this so far: (1 Reply)
Discussion started by: rakeshou
1 Replies

10. Shell Programming and Scripting

getting the stanza names if the pattern found

Hi Friends, I have a file as below : machine1: abc xyz qwerty machine2: jkl mno machine3: hhh kkk qwerty Now ...i need to find all the stanza names that have the pattern "qwerty'" in it...( i need to get the output as machine1 and machine3 since... (4 Replies)
Discussion started by: vijaya2006
4 Replies
Login or Register to Ask a Question