LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 09-10-2012, 01:58 PM   #1
jwilleke
LQ Newbie
 
Registered: Jan 2011
Posts: 7

Rep: Reputation: 0
grep works as commandine but fails as varible


grep '^Sep 7' /var/log/warn
Works at command line but:

teststr='^Sep 7'
grep ${teststr} /var/log/warn

Fails as it returns all lines which start with Sep.

Any ideas?

Thanks
-jim
 
Old 09-10-2012, 02:03 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
You have to put double quotes around the variable reference to protect the blank space(s):
Code:
grep "${teststr}" /var/log/warn
otherwise the command results in
Code:
grep ^Sep 7 /var/log/warn
which makes grep to look for the pattern "^Sep" in a file named "7" and in the "/var/log/warn" file.
 
Old 09-10-2012, 02:26 PM   #3
jwilleke
LQ Newbie
 
Registered: Jan 2011
Posts: 7

Original Poster
Rep: Reputation: 0
That works until it is put in a bash script.
Then it fails to find any entries in the file.

Any ideas?

Thanks
 
Old 09-10-2012, 02:49 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Quote:
Originally Posted by jwilleke View Post
Any ideas?
Nope, until you show the current version of your script. Please use CODE tags to embed the lines of code, so that the correct spacing is preserved. I suspect you missed an additional blank space in the pattern, but I can only guess.

In the meanwhile you can compare your code with a working example tested with grep-2.5.4 and bash-4.1.7:
Code:
#!/bin/bash
#
teststr='^Sep  7'
grep "${teststr}" /var/log/warn
 
Old 09-10-2012, 02:54 PM   #5
YankeePride13
Member
 
Registered: Aug 2012
Distribution: Ubuntu 10.04, CentOS 6.3, Windows 7
Posts: 192

Rep: Reputation: 47
Try adding the full path to the grep command in the script as well.
 
Old 09-12-2012, 10:45 AM   #6
jwilleke
LQ Newbie
 
Registered: Jan 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Thumbs up

Thanks.
With your help and fixing some typos and other errors, we did get this to work.

Code:
#!/bin/bash
REPORTDATE='2012-09-07'
YEAR_MONTH=${REPORTDATE%-*}
YEAR=${YEAR_MONTH%-*}
# Month must be like: Sep 9 from 
MONTHSTR=${YEAR_MONTH#*-}
echo $MONTHSTR
DAYSTR=${REPORTDATE##*-}
# Set MONTHSTR to three letter month
MONTHSTR=`date -d 2012/${MONTHSTR}/01 +%b`
# in the war file, there are two spaces for the month number if 0X 
# and only one if 1X
if [[ $DAYSTR == 0* ]]
then
   SPACE='  '
   echo 'Set two spaces'
   # Strip any leading 0 from DAYSTR
   DAYSTR="$(echo $DAYSTR | sed 's/0*//')"
else
   SPACE=' '
   echo 'Set one space'
fi
teststr="^${MONTHSTR}${SPACE}${DAYSTR}"
/usr/bin/grep "${teststr}" /var/log/warn
Thanks!
-jim
 
Old 09-12-2012, 10:54 AM   #7
YankeePride13
Member
 
Registered: Aug 2012
Distribution: Ubuntu 10.04, CentOS 6.3, Windows 7
Posts: 192

Rep: Reputation: 47
No Problem, Jim. Please mark the thread as solved and feel free to give reputation points to those who you found helpful!
 
  


Reply

Tags
bash scripting


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] grep fails in shell script when string is not found coolnfunky Linux - Newbie 4 05-04-2011 10:02 AM
Code using GREP only works after login. Needs to work on boot up?? sanitynotvanity Programming 3 09-15-2008 08:04 PM
grep fails to install bmfan Linux From Scratch 5 11-14-2005 12:31 PM
grep in shell script fails on redhat 3.0 rlangsto Linux - General 4 03-06-2005 07:42 PM
grep-2.5 static fails ca9mbu Linux From Scratch 1 06-09-2002 10:50 AM


All times are GMT -5. The time now is 12:08 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