LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-17-2007, 07:52 AM   #1
ahjiefreak
LQ Newbie
 
Registered: Dec 2007
Posts: 13

Rep: Reputation: 0
How to check grep status when find elements return 0


Hi,

I would like to know how can we check nonexistence element from grep in a file

k=`grep -n " $char"'$'"" a.txt|tr ":" " "|awk '{print $1}'

Above is the command which I used to grep and check a value of first field element in a file. However, I would like to check if the grep returns me 0 (none found), it would print a constant value of 1000 let say.

I tried to add extra check statement below:-

k=`echo $q |awk '{if($1 == 0) print "0"}'`

However, after I added these, the condition where grep is not "0" could not be printed out.It somehow overlapped the first condition.

Is my syntax somehow wrong which cause the q to update itself. The way I code them is basically if the condition stated fulfill, the value of q shall be updated.

Please advise. Thanks.


-Jason
 
Old 12-17-2007, 08:07 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
RTFM

EXIT STATUS
The following exit values are returned:

0 One or more matches were found.

1 No matches were found.

2 Syntax errors or inaccessible files (even if matches
were found).
 
Old 12-17-2007, 08:08 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,498

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
It can be useful the -q option to grep
Quote:
-q, --quiet, --silent
Quiet; do not write anything to standard output.
Exit immediately with zero status if any match is
found, even if an error was detected.
This means you can use grep to check if a pattern exists and then look at the exit command $?
Code:
test $? -ne 0 && some_commands_here
 
Old 12-17-2007, 04:09 PM   #4
ahjiefreak
LQ Newbie
 
Registered: Dec 2007
Posts: 13

Original Poster
Rep: Reputation: 0
Hi,

But I plan to grep some value from the first condition. If I choose to use -q option, it would not return anything to me.

Given the exist status, I am not sure how I could grab the first field of element which currently I pass to variable "k".

PLease advise. Thanks.
 
Old 12-17-2007, 05:26 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
iirc, if you use a pipeline, the status you get is that of the LAST prog called in the pipeline; in your case awk...
 
Old 12-17-2007, 05:45 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,285
Blog Entries: 54

Rep: Reputation: 2854Reputation: 2854Reputation: 2854Reputation: 2854Reputation: 2854Reputation: 2854Reputation: 2854Reputation: 2854Reputation: 2854Reputation: 2854Reputation: 2854
What he's saying is that with k=($(grep -n " $char"'$'"" a.txt|awk -F':' '{print $1}'`)); [ ${#k[@]} must be -ge 0 ] ||, I think?
 
Old 12-17-2007, 05:57 PM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,498

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Quote:
Originally Posted by ahjiefreak View Post
Given the exist status, I am not sure how I could grab the first field of element which currently I pass to variable "k".
Hi. I would not use a pipeline in this case, better to keep it simple, as in
Code:
k=$(grep pattern file)
if [ $? -eq 0 ]
then
    # pattern found
    # take some actions
else
    # pattern not found
    # take different actions
fi
the code will be also more readable.
 
Old 12-17-2007, 07:14 PM   #8
ahjiefreak
LQ Newbie
 
Registered: Dec 2007
Posts: 13

Original Poster
Rep: Reputation: 0
Hi,

Thanks for the reply.

I tried on my file but has the error of:-
./grep.bash: line 24: syntax error near unexpected token `then'
./grep.bash: line 24: `then'

I tried something like:-

k=$(grep -n " $char"'$'"" c.txt)

if[$? -eq 0];
then
$m=`echo $k|tr ":" " "|awk '{print $1}'`
else
$m=`echo 0`
fi

echo $m

Is there any mistakes I made? The logic seems to be correct.Basically I would like to get the value of the value if found. Else, i would assign it as "0".

Please advise.Thanks.


-Jason
 
Old 12-17-2007, 07:46 PM   #9
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
You need spaces and lose the extra ';' like this:
Code:
if [[ $? -eq 0 ]]
then
    $m=`echo $k|tr ":" " "|awk '{print $1}'`
else
    $m=`echo 0`
fi

echo $m
 
Old 12-17-2007, 09:16 PM   #10
ahjiefreak
LQ Newbie
 
Registered: Dec 2007
Posts: 13

Original Poster
Rep: Reputation: 0
Hi,

Still with error though

./grep.bash: line 27: =: command not found
./grep.bash: line 27: =: command not found
./grep.bash: line 27: =: command not found
./grep.bash: line 27: =: command not found

I code:-

k=$(grep -n " $char"'$'"" c.txt)

if [[ $? -eq 0 ]]
then
$m=`echo 0`
else
$m=`echo $k|tr ":" " "|awk '{print $1}'`
fi

I code the above in a do while loop of reading another file a.txt

Please advise. Thanks.
 
Old 12-17-2007, 10:40 PM   #11
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Well there obviously aren't 27 lines in your code, so we can't help unless you show all of it.
You could try adding
set -xv
at the top of your scipt which will show you in detail exactly what it's doing (kind of a debug mode)
 
Old 12-17-2007, 11:38 PM   #12
ahjiefreak
LQ Newbie
 
Registered: Dec 2007
Posts: 13

Original Poster
Rep: Reputation: 0
Hi,

Sorrry that I missed out the line information:-

Basically I did the below;-

cat a.txt|while read LINE
do

version=`echo "${LINE}"| awk '{print $1}'`
char=`echo "${LINE}"| awk '{print $2}'`

#echo $char
i=`grep -n "^$char" b.txt|awk '{print $2}'`
j=`grep -n "^$char" b.txt|awk '{print $3}'`
#q=`echo $i $j |awk '{printf("%f",($2/($1+$2)))}'`
#k=`grep -n " $char"'$'"" c.txt|tr ":" " "|awk '{print $1}'`

k=$(grep -n " $char"'$'"" c.txt)

if [[ $? -eq 0 ]]
then
$m=`echo 0`
else
$m=`echo $k|tr ":" " "|awk '{print $1}'`
fi

echo $i"\t\t"$j"\t\t"$m">>b.txt

The latest error I got is:-
./grep.bash: line 27: =: command not found
./grep.bash: line 25: =0: command not found
./grep.bash: line 27: =: command not found
./grep.bash: line 27: =: command not found


Which actually is
$k=`echo 0` ->Line 25
$m=`echo $k|tr ":" " "|awk '{print $1}'`->Line 27
From the debug mode; it seems that the K could not print anything if the element is not found yet it gives 1 ? (in bold)
And it also goes to else part as well.

grep -n " $char"'$'"" c.txt
++ grep -n ' 109$' c.txt
+ k=
+ [[ 1 -eq 0 ]]
echo $k|tr ":" " "|awk '{print $1}'
++ echo
++ tr : ' '
++ awk '{print $1}'
+ =
./grep.bash: line 27: =: command not found

Perhaps when it could not find the element in the file, the "K= " is treated as return status as well which gives value 1.

I have checked the matching element case,

grep -n " $char"'$'"" c.txt
++ grep -n ' 108$' c.txt
+ k='1: 2 5 0.959341 108'
+ [[ 0 -eq 0 ]]
echo 0
++ echo 0
+ =0
./grep.bash: line 25: =0: command not found

Please help to advise what would be the best way for solving this problem.

Thanks.
 
Old 12-18-2007, 12:01 AM   #13
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
if $? -eq 0 means it DID match, see BB's post, so your logic is backwards...
BTW, no need to do
$m=`echo 0`

when assigning to a bash var (eg 'm'), do NOT prefix var name with '$', only when reading it.
Also, just assign '0' directly like:
m=0

or

m=`echo $k.....`
etc
 
Old 12-18-2007, 01:25 AM   #14
ahjiefreak
LQ Newbie
 
Registered: Dec 2007
Posts: 13

Original Poster
Rep: Reputation: 0
Hi,

I somehow understand from your reply. Would try it out and see whether does it gives me the error again.

Meanwhile, I have another question related;

I face a problem where I could not able to grab the element value like as below in a file, a.txt

a.txt

15 1 2
150 2 3
156 3 4

I would like to grep value of 15 and also use the field. However, currently I have problem where grep would also grab the 150 and 156 as well.

My grep looks like this:-
#grab the particular value let say "15" in this case in file a.txt
i=`grep -n "^$char" a.txt|awk '{print $2}'`
echo $i

Output desired
1

Current Output
1
2
3

When I added a $ at the back to uniquely identify the number, the second field is truncated.

Please help. Thanks.
 
Old 12-18-2007, 01:33 AM   #15
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,498

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Quote:
Originally Posted by ahjiefreak View Post
I would like to grep value of 15 and also use the field. However, currently I have problem where grep would also grab the 150 and 156 as well.
You can try by adding the -w option to the grep command. See man grep for details.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
grep detecting carriage return, how ? Grafbak Programming 22 01-13-2010 01:19 PM
How do you return a value with grep ? pppaaarrrkkk Linux - Newbie 2 11-30-2007 06:37 PM
Official command return status specification?? kinzlaw Linux - General 1 03-11-2006 02:38 AM
Multi-line return from grep into an array? rose_bud4201 Programming 2 06-14-2005 04:11 PM
undefined refrence return status link error azucarmom Programming 7 03-30-2005 11:15 AM


All times are GMT -5. The time now is 05:10 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration