Execution problem with grep script (2 variables)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Execution problem with grep script (2 variables)
# 8  
Old 06-02-2010
You don't need to use eval - or egrep if you escape the infix |

Of course it runs... maybe it just doesn't find anything?

Show some of your input file

Code:
$ echo cats and dogs > file1
$ GREP=$(which grep)
$ $GREP "cats\|dogs" file1
cats and dogs

This User Gave Thanks to Scott For This Post:
# 9  
Old 06-03-2010
Quote:
Originally Posted by pseudocoder
What does that mean in concrete terms? Is there no output?

Did you try to run that command in the shell and see if it will work?
Code:
/usr/bin/grep -i 'dog\|cat' test.log

Also try
Code:
/usr/bin/grep -iE 'dog\|cat' test.log

Code:
 grep -i 'dog\|cat' test.log

works in the shell! but /usr/bin/grep -i 'dog\|cat' test.log doesn't

---------- Post updated at 11:03 PM ---------- Previous update was at 10:59 PM ----------

Test file for grep input
Code:
this is a cat.
this is a dog.
this is a fox.
this is a bird.
this is a frog.
And cat and dogs are Man's best friends.

I made the following changes and the script now runs fine:

Code:
#!/bin/sh
TEST=animal.log

eval "grep -i 'dog\|cat' ${TEST}"

output:
Code:
this is a cat.
this is a dog.
And cat and dogs are Man's best friends.

i then do a whereis grep:

Code:
grep: /usr/bin/grep /usr/local/bin/grep /usr/man/man1/grep.1

I really don't understand why
Code:
#!/bin/sh
TEST=test.log
GREP=/usr/bin/grep

eval "$GREP -i 'dog\|cat' ${TEST}"

doesn't work
# 10  
Old 06-03-2010
Standard grep uses POSIX BRE's (Basic regular expressions) which do not include alternation. So one should use egrep for that which supports ERE's (Extended Regular Expressions). The reason that \| works with grep for some is that GNU grep added this as an extension to BRE, but this is not standard.
This User Gave Thanks to Scrutinizer For This Post:
# 11  
Old 06-03-2010
Quote:
Originally Posted by Scrutinizer
Standard grep uses POSIX BRE's (Basic regular expressions) which do not include alternation. So one should use egrep for that which supports ERE's (Extended Regular Expressions). The reason that \| works with grep for some is that GNU grep added this as an extension to BRE, but this is not standard.
thanks you for the detailed explanation about "\|", after using egrep instead, everything works out fine Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Execution problem with shell script for modifying a user

#/bin/sh echo "enter the user name" read $username echo "Enter new home directory" read $newhd usermod -d $newhd $username ;; error while executing : enter the user name Rev Enter new home directory: /home/58745 usermod: option requires an argument -- 'd' Try `usermod --help' or... (2 Replies)
Discussion started by: Revanth547
2 Replies

2. Solaris

Execution problem in shell script while insert into DB

Hi, am facing some problem while inserting a record into a script Please find script below. `sqlplus -s asdf/asdf123 <<eof! set feedback off; set heading off; set verify off; insert into... (2 Replies)
Discussion started by: senkerth
2 Replies

3. IP Networking

Problem with script execution from a DHCP event

Hi, I'm installing a DHCPD Server with the packages from a distro SLES11 SP1 (dhcp-server-3.1.1-7.12). And a DNS Server with PowerDNS: pdns-recursor-3.3-1 pdns-static-3.0-1 The DHCP update de DNS Server, but PowerDNS is not RFC 2135 compliant, and I have to update the MySQL register... (5 Replies)
Discussion started by: bypper
5 Replies

4. Shell Programming and Scripting

execution problem with grep

how to use grep word from sentence grep -o "hai" haighaihaihai Is above cmd possible in linux ? can any one help me? Thanks, (5 Replies)
Discussion started by: kavi.mogu
5 Replies

5. Shell Programming and Scripting

Execution problem with csh script

Hi All, I have a small issue with my csh script which I am using to FTP a file. What I know is...there are two commands to execute script.. 'sh <file>' & '\<file>'. When I execute my script with command 'sh <file>', it gives me syntax error while it runs successfully with command '\<file>'. I am... (3 Replies)
Discussion started by: ndd
3 Replies

6. Shell Programming and Scripting

Execution problem with shell script

Hi all, I want to use perl string manipulation commands in my shell script. I have written following script. echo "enter name" read name perl -e '$m=length($name); echo $m it gives an error: unrecognized token in perl command line. do not suggest me an equivalent command of shell... (3 Replies)
Discussion started by: admc123
3 Replies

7. Shell Programming and Scripting

parallel execution of script/ synchro problem

Hi everybody, In a csh script, i need to run 4 time the same prog with different parameters. What i want is to run them in parallel. for this i use the command toto1.sh & toto2.sh & toto3.sh & toto4.sh For this I have no problem. In fact, I need to wait until all the programs are over to... (2 Replies)
Discussion started by: Moumou
2 Replies

8. Shell Programming and Scripting

problem with remote execution of script using telnet

Hi all, i am trying to remotely execute a script from a different server. this is the code that i use : #!bin/sh pwd (sleep 1 echo "username" sleep 2 echo "pwd" sleep 2 echo "cd /path/to/file" if then echo "script1.sh" echo "mailx -s "Task Executed"... (1 Reply)
Discussion started by: sais
1 Replies

9. Shell Programming and Scripting

problem with shell script execution

Hi All, i am running a shell script in which there is a command `ps -ef | grep smon > db` When i execute this command in the command prompt i am getting the desired output..but when the script is executed..the db file is getting created but with no values...I could not find the reason for... (2 Replies)
Discussion started by: anju
2 Replies

10. AIX

Pb with script execution and variables

Hello, Can somebody tell me the differnce between a call of a ksh with the dot : >. script.ksh and the call without the dot: >script.ksh In my script I have writen a test for the number of parameters: if then echo 'Usage : '$0 exit 1 fi as my script doesn't need any... (1 Reply)
Discussion started by: Cecile
1 Replies
Login or Register to Ask a Question