Unix/Linux Go Back    


UNIX for Beginners Questions & Answers If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

How to grep variable in shell script?

UNIX for Beginners Questions & Answers


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by scriptor
scriptor's Unix or Linux Image
scriptor scriptor is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 9 April 2018, 9:00 AM EDT
Posts: 180
Thanks: 21
Thanked 0 Times in 0 Posts
How to grep variable in shell script?

Hi Team,

I am trying to grep date in a file but it is not working



Code:
#!/bin/bash
d=`date "+ %Y%m%d %H:%M"`
cd /scripts
/bin/rm -f test1
cat /var/logs/File.txt.0 |grep $"d" >v.txt

instead it is showing the complete file output.
kindly suggest how should i grep this variable

it works fine if I search manually


Code:
 
 cat /var/logs/File.txt.0 |grep "20180321 18:54"| head
 [20180321 18:54:03.196000] Module:
1 Aff.Obj: Info: 
[20180321 18:54:17.074395] Module:
j: 10.239.128.10:6789 Info: 
[20180321 18:54:33.197000] Module:

scriptor

Last edited by scriptor; 4 Weeks Ago at 09:44 AM.. Reason: added my observation
Sponsored Links
    #2  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by scriptor
RudiC's Unix or Linux Image
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 April 2018, 5:23 AM EDT
Location: Aachen, Germany
Posts: 12,507
Thanks: 401
Thanked 3,878 Times in 3,565 Posts
Try "$d".
Sponsored Links
    #3  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by scriptor
apmcd47's Unix or Linux Image
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 12 April 2018, 11:24 AM EDT
Posts: 336
Thanks: 16
Thanked 92 Times in 87 Posts


Code:
#!/bin/bash
d=`date "+ %Y%m%d %H:%M"`
cd /scripts
/bin/rm -f test1
fgrep "${d}" /var/logs/File.txt.0 >v.txt

You DON'T need to cat a file into grep - grep can pick up a file itself.

You got $d wrong. It should have been "$d" or preferably (as I have put) "${d}". You put $"d".

I used fgrep here instead of grep as that searches for strings, rather than regular expressions.

Andrew
    #4  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by scriptor
scriptor's Unix or Linux Image
scriptor scriptor is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 9 April 2018, 9:00 AM EDT
Posts: 180
Thanks: 21
Thanked 0 Times in 0 Posts
Hi Andrew/Rudic,

I tried both the option suggested by you.
however I am not getting any output.

please suggest.



Code:
 
 # set -x
# cat /var/logs/File.txt.0 |grep "${d}" | head 
+ grep ' 20180322 11:56'
+ head
+ cat /var/logs/File.txt.0



Code:
 
 # set -x
# echo $d
+ echo 20180322 11:56
20180322 11:56
# fgrep "${d}" /var/logs/File.txt.0 | head 
+ head
+ fgrep ' 20180322 11:56' /var/logs/File.txt.0
#

Sponsored Links
    #5  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by scriptor
RudiC's Unix or Linux Image
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 April 2018, 5:23 AM EDT
Location: Aachen, Germany
Posts: 12,507
Thanks: 401
Thanked 3,878 Times in 3,565 Posts
You have a leading space in d that doesn't seem to exist in your file.
Sponsored Links
    #6  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by scriptor
scriptor's Unix or Linux Image
scriptor scriptor is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 9 April 2018, 9:00 AM EDT
Posts: 180
Thanks: 21
Thanked 0 Times in 0 Posts
Hi

I don't see any space in my file.

also I tried again still not working.


Code:
#d=`date "+ %Y%m%d %H:%M"`
++ date '+ %Y%m%d %H:%M'
+ d=' 20180322 14:14'
#
#
# cat /var/logs/File.txt.0 |grep "$d"
+ grep ' 20180322 14:14'
+ cat /var/opt/fds/logs/EventLogFile.txt.0

#

Last edited by scriptor; 4 Weeks Ago at 04:51 AM.. Reason: added tagg for code
Sponsored Links
    #7  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by scriptor
apmcd47's Unix or Linux Image
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 12 April 2018, 11:24 AM EDT
Posts: 336
Thanks: 16
Thanked 92 Times in 87 Posts
Quote:
Originally Posted by RudiC View Post
You have a leading space in d that doesn't seem to exist in your file.
Quote:
Originally Posted by scriptor View Post
Hi

I don't see any space in my file.

also I tried again still not working.


Code:
#d=`date "+ %Y%m%d %H:%M"`
++ date '+ %Y%m%d %H:%M'
+ d=' 20180322 14:14'
#
#
# cat /var/logs/File.txt.0 |grep "$d"
+ grep ' 20180322 14:14'
+ cat /var/opt/fds/logs/EventLogFile.txt.0

#
The bit in red. Rudi said the space was in d.
Try this instead:


Code:
d=`date "+%Y%m%d %H:%M"`

Andrew
The Following User Says Thank You to apmcd47 For This Useful Post:
scriptor (4 Weeks Ago)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Unable to pass shell script variable to awk command in same shell script Ashunayak Shell Programming and Scripting 2 01-27-2014 12:04 PM
Issue with passing variable to Grep in a shell script racshot65 Shell Programming and Scripting 1 08-25-2011 09:48 AM
assign awk's variable to shell script's variable? tiger2000 Shell Programming and Scripting 4 04-13-2010 03:53 PM
Shell script / Grep / Awk to variable and Loop Spoonless Shell Programming and Scripting 0 01-26-2010 08:11 AM
Grep results to store in a shell variable jojan Shell Programming and Scripting 3 07-26-2007 12:44 PM



All times are GMT -4. The time now is 05:33 AM.