grep functions, how to test if succeeded


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep functions, how to test if succeeded
# 8  
Old 11-28-2009
Oops Smilie

Last edited by Scrutinizer; 11-28-2009 at 07:39 AM..
# 9  
Old 11-28-2009
I don't really get that... the -q option states: Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option. (-q is specified by POSIX.)

So that doesn't make sense, additionally I ran a search which even made me more complexed:
Five Shell Programming Tips

it says exit with zero when match is found, not the other way around... anyone care to enlighten me?
# 10  
Old 11-28-2009
double post, this site has been screwy today ^_^

EDIT: ok, nevermind I just got it, testing for a line in a file.... which is true so we delete. duh...

EDIT: why does my head always feel like it's going to explode over the simplest of things, yet I can usually grasp complex >.<

---------- Post updated 11-29-09 at 12:32 AM ---------- Previous update was 11-28-09 at 09:58 PM ----------

I don't want to start yet another new thread, this is simply a lame output format I can't seem to get around.

I'm greping line for delete function, and then I want to display the whole line to standard output with the string ":deleted" appended.

I have tried piping grep to echo, but have tried all positional parameters $1 $? $@ and no joy as it only returns 0, which seems to be what is output in echo. using redirection gives me the line "grep: BREAD - WHITE:deleted"

not sure how to get around this unless i grep the output to temp file then cat the file.

I need it to say the full line that grep would output such as:
BREAD - WHITE:TIPTOP:5:deleted.

but as I am only searching for "BREAD - WHITE:TIPTOP:" (the only arguments i have) and not the isle number I can't get it to output the isle number >
I also tried
grep "string" $file ; echo ":deleted."
but this put the echo on separate line.

Last edited by gcampton; 11-28-2009 at 10:39 AM..
# 11  
Old 11-28-2009
How about:
Code:
if record=$(grep "$1$2" $dbfile)
then 
    #delete the line
    echo "$record:deleted."
else
    echo "Key $1$2:No such record"
fi

# 12  
Old 11-28-2009
Thanks again scrutinizer (and others) you have definately taught me a lot over the last few days. Only have only last "ambiguous redirect" error occurring which I should be able to figure out and I'm done, Smilie 3 week assignment done in 3 days woooot.

---------- Post updated at 08:32 AM ---------- Previous update was at 08:24 AM ----------

Jumped the gun there, this is not working
Code:
if record=$(grep -q "${1}:${2}:" $dbfile)
 "${1}:${2}:" then
    grep -v  "${1}:${2}:" $dbfile > ../tmp/tmp
    cat ../tmp/tmp > $dbfile
    echo "${record}${RECDELETED}"
else
    echo "${1}:${2}:${ERRNODEL}"
fi

also tried
Code:
 
if record=(grep -q "${1}:${2}:" $dbfile)

output of the first is ":deleted" second is "grep:deleted"
# 13  
Old 11-28-2009
Quote:
if record=$(grep -q "${1}:${2}:" $dbfile)
"${1}:${2}:" then
My example was without the -q option, otherwise $record will be empty Smilie
I suspect the part in red is a cut-pasto?

S.

Last edited by Scrutinizer; 11-28-2009 at 06:48 PM..
# 14  
Old 11-28-2009
woops.. misread,

thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is it correct? if test grep EOF $a ...

read a if test grep EOF $a then echo yes file else echo no fi (1 Reply)
Discussion started by: iamsumibisht
1 Replies

2. Shell Programming and Scripting

Using grep with test and without using [[ ]]

As an exercise, I'm trying to re-write this code without the compound square brackets, using grep and test. Need to know what to do about the "equal-tilde". #!/bin/bash # test-integer2: evaluate the value of an integer. INT=-5 if +$ ]]; then if ; then echo "INT is zero." else if ; then... (17 Replies)
Discussion started by: Xubuntu56
17 Replies

3. Shell Programming and Scripting

Unable to check if my command succeeded

Hi, Is there a way to check if the openssl part of the below execution returns non zero without freezing the flow of the script ? var1=$(echo | openssl s_client -connect $url 2>/dev/null | openssl x509 -noout -issuer -subject -dates) echo $? Output: 0 while i was expecting the... (2 Replies)
Discussion started by: mohtashims
2 Replies

4. Shell Programming and Scripting

Grep/print/ a test file

cat abc.txt Filename: SHA_AED_Monthly_SNR_20150331.txt.gz Data Format: ASCII with carriage returns and linefeeds Compression: GZIP GZIP Bytes: 36893068 Unzipped Bytes : 613794510 Records: 851310 Record Length: 738 Blocksize: 32472 Filename: SHA_AED_SNR_ChangeLog_20150331.txt.gz Data... (16 Replies)
Discussion started by: dotran
16 Replies

5. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

6. Shell Programming and Scripting

If test grep.... always returns 0 status

Hi all. I am trying to compare and filter two files. I have a bigfile.txt of names and ids and a smallfile.txt of ids only. What I am trying to do is use a while read loop to read the ids in the bigfile and then echo the name and id only if the id exists in the small file. Basically, I'm trying to... (5 Replies)
Discussion started by: jameswatson3
5 Replies

7. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

8. Shell Programming and Scripting

Using grep inside a test

Hi, I want to use grep inside a test statement, but I am getting an error message. Two variables testvarNum=5 testvarNonNum=x echo $testvarNum | grep * The result of this is as follows: 5 However, when I try the following (i.e. to test if the variable is numeric or non-numeric):... (3 Replies)
Discussion started by: dkieran
3 Replies

9. UNIX for Dummies Questions & Answers

Unix grep/test command

Hello, i have a script which checks if the user entered 8 numeric characters in the form of YYYYMMDD (birth date). If the user entered any non numeric characters, an error will be displayed: # Check to see if the 8 characters are all numbers # If not show error essage # And prompt user... (4 Replies)
Discussion started by: netmaster
4 Replies

10. Shell Programming and Scripting

Using grep in a test/if statement

Okay, well this is more or less my first attempt at writing a shell script. Anyways, here's my code: cd ${PATH} if then rm ${FILE} ./anotherScript else exit 1 fi exit 1 Anyways, it's a pretty simple script that is supposed to search for the... (4 Replies)
Discussion started by: cbo0485
4 Replies
Login or Register to Ask a Question