Issues with grep -w


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issues with grep -w
# 1  
Old 08-12-2009
Issues with grep -w

I am trying to use grep -w something as shown below.
Code:
grep -w "$a" $LOG1

It is not giving me neither any error nor any data. I am facing the issue while i run this command inside a script. But i am getting the data if i run the above command outiside the script.
here $a=08/11/2009 21
i tried outisde something like this and is working.
grep -w "08/11/2009 21" $LOG1

Can someone help me with the grep -w inside the script?
# 2  
Old 08-12-2009
Try this

Code:
grep -w $a $LOG1

# 3  
Old 08-12-2009
Giving an error message as it is treating 21 as the file name.

---------- Post updated at 11:27 PM ---------- Previous update was at 11:18 PM ----------

It it is working fine in the script also if i input the data manually like as i shown below.
a='08/11/2009 21'
export a

But it is not working if i am use the script shown below for getting the value into variable $a. Even though i am able to echo $a correctly.

Code:
#!/bin/bash
genexpression()
{
stime=`date '+ %H'`
#stime=4;
if (( $stime < 5))
then
stime=`expr $stime \+ 19`
export express=`TZ=CST+24 date '+ %m/%d/%Y'`\ $stime
#stime=`expr $stime \- 5`
else
stime=`expr $stime \- 5`
if (( $stime < 10 ))
then
stime=0${stime}
fi
export express=`date '+%m/%d/%Y'`\ $stime
fi
}
genexpression
echo $express
a=$express
export a
echo $a
grep -w "$a" LOG.txt


Last edited by rdhanek; 08-12-2009 at 12:54 AM..
# 4  
Old 08-12-2009
While using only the last part from your script, works fine for me.

Code:
a='08/11/2009 21'
echo $a
grep -w "$a" t1


Kindly make the above as the new script, and check what is the behavior, and report it here.

And why are you doing all the above thing ?? [ that is manipulating with the date command, and finally not using it !! ]

Kindly make the issues very clear, by pasting the output also.
# 5  
Old 08-12-2009
I am sorry for the confusion. That line a=08/11/2009 21' should be replaced with a=$express.I will update my post also accordingly.
And i am using the total script to get 5 hrs back data. And the date format in the file is something like this
08/11/2009 09:00:00

---------- Post updated 08-12-09 at 01:37 AM ---------- Previous update was 08-11-09 at 11:52 PM ----------

If it goes to the else condition The above code is working fine. Seems to be an issues with white apces in if part of the code.
# 6  
Old 08-12-2009
Hi rdhanek,

The same script what you have posted working perfectly for me.

Last edited by panyam; 08-12-2009 at 03:14 AM..
# 7  
Old 08-12-2009
Hi Panyam,

here is the code that i am using to grep for 5 hrs back data. As a part of it..if it goes to the else loop it is working fine. but the data is picked by grep if the code executes th efirst pasrt of teh code(i.e, if loop suuceeds)
p.s. In both the cases i am able to echo date format correctly. Not sure this may be a space issue.

Code:
#!/bin/bash
genexpression()
{
stime=`date '+ %H'`
if (( $stime < 5 ))
then
stime=`expr $stime \+ 19`
export express=\(`TZ=CST+24 date '+ %m/%d/%Y'`\ $stime
else
stime=`expr $stime \- 5`
if (( $stime < 10 ))
then
stime=0${stime}
fi
export express=`date '+%m/%d/%Y'`\ $stime
fi
}
genexpression
export express
echo $express
t=`grep -w "$express" Service.log | wc -l`

if [ $t -ne 0 ];then
echo "suces"
else
echo "fail"
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Echo and grep issues

Is there an environment issue that would not allow the following not store and pass the value into this field: underScorePresent=`echo $USER | grep "_" | wc -l` It is running on a new redhat 6.5 OS. The value $USER is set to cpac. It is a vendor code and they are saying it is environment... (1 Reply)
Discussion started by: mrn6430
1 Replies

2. Shell Programming and Scripting

Inconsistent `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`

i have this line of code that looks for the same file if it is currently running and returns the count. `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l` basically it is assigned to a variable ISRUNNING=`ps -eaf -o args | grep -i sfs_pcard_load_file.ksh |... (6 Replies)
Discussion started by: wtolentino
6 Replies

3. UNIX for Dummies Questions & Answers

Piping grep into awk, read the next line using grep

Hi, I have a number of files containing the information below. """"" Fundallinfo 6.3950 14.9715 14.0482 """"" I would like to grep for Fundallinfo and use it to read the next line? I ideally would like to read the three numbers that follow in the next line and... (2 Replies)
Discussion started by: Paul Moghadam
2 Replies

4. UNIX for Dummies Questions & Answers

Issues while pattern matching using grep

Hi, I have a file f1 wi the following data f1.txt ======== Report ID Report Name ----------------------------------------------------------------- Post Requests : 2 Post successes : 2 ============================================= I need to search for the... (2 Replies)
Discussion started by: RP09
2 Replies

5. UNIX for Dummies Questions & Answers

Bash - CLI - grep - Passing result to grep through pipe

Hello. I want to get all modules which are loaded and which name are exactly 2 characters long and not more than 2 characters and begin with "nv" lsmod | (e)grep '^nv???????????? I want to get all modules which are loaded and which name begin with "nv" and are 2 to 7 characters long ... (1 Reply)
Discussion started by: jcdole
1 Replies

6. Shell Programming and Scripting

Issues in grep command in Linux

Hi All I have a file containing following records: $HEW_TGT_DB2_USER=hbme_bi2 $prmAttunityUser=ais $DS_USER=hbme_bi2 $prmStgUser=hbme_bi2 $prmuser=hbme_bi2 $prmStgPass=hbme_bi2 $prmpwd=hbme_bi2 $prmAttunityUser=ais Say suppose the name of the file is test4.txt When i fire this... (5 Replies)
Discussion started by: vee_789
5 Replies

7. Shell Programming and Scripting

grep for certain files using a file as input to grep and then move

Hi All, I need to grep few files which has words like the below in the file name , which i want to put it in a file and and grep for the files which contain these names and move it to a new directory , full file name -C20091210.1000-20091210.1100_SMGBSC3:1000... (2 Replies)
Discussion started by: anita07
2 Replies

8. UNIX for Dummies Questions & Answers

| help | unix | grep (GNU grep) 2.5.1 | advanced regex syntax

Hello, I'm working on unix with grep (GNU grep) 2.5.1. I'm going through some of the newer regex syntax using Regular Expression Reference - Advanced Syntax a guide. ls -aLl /bin | grep "\(x\)" Which works, just highlights 'x' where ever, when ever. I'm trying to to get (?:) to work but... (4 Replies)
Discussion started by: MykC
4 Replies

9. Shell Programming and Scripting

MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else

Hi Guys, I need to set the value of $7 to zero in case $7 is NULL. I've tried the below command but doesn't work. Any ideas. thanks guys. MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else { print $7}}' ` Harby. (4 Replies)
Discussion started by: hariza
4 Replies

10. UNIX for Dummies Questions & Answers

GREP issues

Hi all, I am trying to perform a simple task with grep, this is what I execute: grep -i -n 'error|fail|warning' cl_less.out when I execute it it does not give any errors, as in the command worked and back to the command prompt, but if I run each grep independently the I do get a real fail: ... (8 Replies)
Discussion started by: sqloyd
8 Replies
Login or Register to Ask a Question