awk with exit status if pattern not found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk with exit status if pattern not found
# 1  
Old 06-01-2012
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 exists and flag the computer as being failed in our database.

I am pretty good with my bash skills but awk and sed still hang me up from time to time and I cannot wrap my head around this one so figure I'd ask the gurus here, as I am probably just being an idiot on how this works.

Here is an example:

Code:
#!/bin/bash

sourceFile="/path/to/file/with/patterns"

if [[ awk -F, '/pattern/ {print $2, exit}' ${sourceFile} == new_pattern ]]
then do various tasks
else echo "renaming failed" > /var/logs/failure.log
exit 1
fi

new_pattern is determined by a variable parsing through the source file. I am still testing out various ways of doing this. Find command, data array, and also sourcing the list of proper names. The file is comma separated. I am also passing the /pattern/ part from bash as a variable. That works.

This is hanging up so I think I need to do he if statement in awk and not use bash. However, if the pattern matches the script works fine. This is great, but I need some way to track down failures down the road and manually fix them. Basically, I work for a company that has merged with other companies and having to bring everything up to the new parent company's standards (naming conventions of files, folders, etc) So I am matching patterns of what the standards should be, versus what they are currently named (supplied via master text file from remote offices) and then trying to automate the process. Tons of computers out in the wild right now. There are going to be failures because of certain unknowns.

So, I am trying some if statements in awk instead:

Code:
awk -F, 'if ($1 ~ /^$pattern/){ print $2, exit }' ${sourceFile};
else exit 1

This seems to not be working as I intended. Sorry for the probably easy noob question but I am not wrapping my head around this. Currently reading through my awk books as well.

Thanks a ton in advance
# 2  
Old 06-01-2012
Just making awk exit early doesn't make it return error, you need 'exit 1'.

Variables don't expand inside single quotes, either.

Code:
if awk -v P="$pattern" 'P ~ $0 { exit 1 }'
then
        echo "Awk succeeded"
else
        echo "Awk returned error"
fi

Though you could as easily use grep with inverted logic, as it will return 1 when not found and 0 when found:

Code:
if grep -l "$pattern" filename > /dev/null
then
        echo "Pattern found in file"
else
        echo "Pattern not found in file"
fi

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 06-01-2012
Wow, you're good. Yes, my script expands the variable properly with the proper quotes I just didn't copy/paste the right from my script.

Also, running grep beforehand to make sure those are in the master list is actually an easier approach, and I knew that but didn't think of it. Again, good thinking.

I am thinking of this:
Code:
grep -l "pattern" /path/to/inputfile.txt

if [[ `/bin/echo "$?"` -eq 0 ]]
then echo "pattern found"
else echo "pattern not found..."
echo "procedure failed" > /var/log/failure.log
exit 1
fi

Unless there is a good reason I should not check the exit status of grep?

This at least gives me something to work with. Thanks again mate!
# 4  
Old 06-02-2012
You can also use the return code of grep directly:
Code:
if grep -l "pattern" /path/to/inputfile.txt
then

To suppress output of grep:
Code:
if grep -q "pattern" ...

or
Code:
if grep "pattern" ... > /dev/null 2>&1

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

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

3. 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

4. Shell Programming and Scripting

Exit Status

I have a shell script (#!/bin/sh) that interacts with Appworx and Banner Admin. In my script I want to check the exit status of awrun before continuing. awrun can run for 10 seconds or it can run for over a minute. So my question is, will it go through my if statement before awrun may even be... (2 Replies)
Discussion started by: smkremer
2 Replies

5. Shell Programming and Scripting

How to print exit status in AWK

Hi all, How can I print the exit status in AWK? echo $? doesnt work for me Thanks (4 Replies)
Discussion started by: Pauline mugisha
4 Replies

6. 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

7. Shell Programming and Scripting

Exit status

I'm preparing for exam and one of exams is to write own test command... I wonder if in unix is a command which just returns exit code you specify.. I know I can easily write a function like this: exStatus() { return $1 } -> my question is rather theoretical thank you! (9 Replies)
Discussion started by: MartyIX
9 Replies

8. Shell Programming and Scripting

Pattern not found : AWK , help

Hi All, My file : $ 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... (7 Replies)
Discussion started by: jkl_jkl
7 Replies

9. UNIX for Dummies Questions & Answers

how to check exit status in awk script

Hi, I have a main program which have below lines - awk -f test.awk inputFileName - I wonder how to check status return from awk script. content of awk script: test.awk --- if ( pass validation ) { exit 1 } else { (1 Reply)
Discussion started by: epall
1 Replies

10. Shell Programming and Scripting

exit status

i downloaded a text file from metalab.unc.edu called sh.txt and in this reference manual it refers to shell scripting exit status .. at the end of one of the examples that author gave an exit status of 127.. to what does a 127 exit status refer too and what is its purpose in the code. moxxx68 (1 Reply)
Discussion started by: moxxx68
1 Replies
Login or Register to Ask a Question