Grep: Return a user message when no hits


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep: Return a user message when no hits
# 1  
Old 06-13-2011
Grep: Return a user message when no hits

I have ASCII files to parse that 48 hours old or more ; I can identify them like so

Code:
    find . -name "FILE*TXT"  -mtime +1 -exec ls -ltas '{}' ';'

Some files have a list of hardware errors (we test electronic components), some have none. If the file name has no errors, I still want to display a message like so

Code:
        grep ^err R*VER && echo "No error"
    FILEA.TXT:err	->USB	3910	err	
    FILED.TXT:err No Error

This grep statement works but it seemingly overrides the find() statement above if I run both at the same time... How can I combine the two statements to create a report that lists the filename and error(s) like so

Code:
    FILEA.TXT Button	3320	err
    FILEB.TXT USB	3235	err
    FILEC.TXT IR Remote	2436	err
    FILED.TXT No error

Is it possible to return "No error" with the file name without error? Thanks in advance for your help.
# 2  
Old 06-13-2011
Code:
find . -name "FILE*TXT"  -mtime +2 |while read file
do
  grep ^err $file && echo "No error"
done

# 3  
Old 06-14-2011
After running the command

Code:
find . -name "R*VER" -mtime +1 -exec sh -c 'grep -H ^err "{}" || echo "{}:No error"' \;

my files look like this

Code:
RRR1~COS~COSMETICS~40048~jgmdtv113~1~P~R22-200~029053662549~20110607~102151.VER No error
RRR1~COS~COSMETICS~ETT03~jgm14652.~3~F~R16-500~000907009757~20110607~085109.VER err 3922 	
...

Using the tilde (~) symbol as delimiter, can I extract the fields I want so I get an output like this

Code:
RRR1~COS~COSMETICS~40048~jgmdtv113~1~P~R22-200~029053662549~20110607~102151.VER No error RRR1 COS P
RRR1~COS~COSMETICS~ETT03~jgm14652.~3~F~R16-500~000907009757~20110607~085109.VER err 3922 RRR1 COS F 	
...

I tried the following

Code:
find . -name "R*VER" -mtime +1 -exec sh -c 'grep -H ^err "{}" || echo "{}:No error"' \;|awk -F~ '{print $0}{print $1"\t"$2"\t"$7"\t"$8"\t"$9"\t"$10}'

but it does not work; Instead, it produces this output (not all fields shown here...)

Code:
RRR1~COS~COSMETICS~40048~jgmdtv113~1~P~R22-200~029053662549~20110607~102151.VER No error
RRR1~COS~COSMETICS~40048~jgmdtv113~1~P~R22-200~029053662549~20110607~102151.VER RRR1 COS P
RRR1~COS~COSMETICS~ETT03~jgm14652.~3~F~R16-500~000907009757~20110607~085109.VER err 3922
RRR1~COS~COSMETICS~ETT03~jgm14652.~3~F~R16-500~000907009757~20110607~085109.VER RRR1 COS F

I'd like to do this is one pass so I can generate a SQL script with INSERT statements...Can it be done?
# 4  
Old 06-16-2011
Code:
find . -name "R*VER" -exec sh -c 'grep -H ^err "{}" || echo "{}:No error"' \; |
        awk -F: '
                BEGIN { q="\047" }
                {
                        file=$1
                        result=$2
                        sub(/^.*\//,"",file)
                        split(file,a,/~/)
                        print "INSERT INTO MYTABLE (COL1,COL2,COL3,COL4,COL5,COL6,COL7,COL8) VALUES (" q file q "," q result q "," q a[1] q "," q a[2] q "," q a[7] q "," q a[8] q "," q a[9] q "," q a[10] q ")"
                }
        '

Took care of it.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep for list of files with only found hits

Hi, With grep -ci word * I get a list of files either they have a hit, or the have not (0). I wanted to pipe to a new list of files, which only shows the files where the string was found, and counted, not the whole bunch. How to do this? Any advice welcome! with best regards, Omar... (11 Replies)
Discussion started by: OmarKN
11 Replies

2. Shell Programming and Scripting

How to return a message after command runs?

I have a script like this: echo "enter filername in lowercase" read -e filername exec 2>&1 echo "type the start date in format MM/DD/YYYY" read -e startdate exec 2>&1 echo "enter the end date in format MM/DD/YYYY" ... (2 Replies)
Discussion started by: newbie2010
2 Replies

3. UNIX for Advanced & Expert Users

return of grep

Hi, in a script I will do : grep ORA- mylogfile.log If it returns any ORA- (oracle error) I whant to have : echo "subject : RMAN in Error " >> /appli/rap.txt But if no error echo "subject : RMAN OK " >> /appli/rap.txt Can you help me ? The grep return code would it be... (1 Reply)
Discussion started by: big123456
1 Replies

4. Shell Programming and Scripting

grep return more than one line

Hi, I have a lot of log files which contain lines in the format of (date info) or (info), and when I use grep to search for "date" I was able to get the (date info) line, but some info lines are a separate line after the (date info) line... for example like: (date info info info) (date... (8 Replies)
Discussion started by: a27wang
8 Replies

5. UNIX for Dummies Questions & Answers

| help | unix | grep - Can I use grep to return a string with exactly n matches?

Hello, I looking to use grep to return a string with exactly n matches. I'm building off this: ls -aLl /bin | grep '^.\{9\}x' | tr -s ' ' -rwxr-xr-x 1 root root 632816 Nov 25 2008 vi -rwxr-xr-x 1 root root 632816 Nov 25 2008 view -rwxr-xr-x 1 root root 16008 May 25 2008... (7 Replies)
Discussion started by: MykC
7 Replies

6. UNIX for Dummies Questions & Answers

Return mail message

Hi I would like to know how interpret line by line a return mail message sent by unix mailer-daemon (2 Replies)
Discussion started by: alcvrjo
2 Replies

7. Shell Programming and Scripting

problem in the return of grep

hi, i am running this command inside the script var=`grep -il $1 "${logdir}"* | xargs grep -ivl adding | xargs grep -ivl equation | xargs ls -ctr | tail -1` in that grep will find the latest logfile for the variable "$" if it fnd the logfile, then it reutrns the filename to the var if... (1 Reply)
Discussion started by: mail2sant
1 Replies

8. Shell Programming and Scripting

Return a message when a file is not found

Hi there, I am writing a script to look for tmp log files that have not been access within the last 10 days. I am using the follwing command within the script: find /var/tmp -name *log -atime -9 ¦xargs What I would like to be able to do would be to display a message if there is no... (3 Replies)
Discussion started by: lodey
3 Replies
Login or Register to Ask a Question