How to grep variable in shell script?

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 03-21-2018
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; 03-21-2018 at 10:44 AM.. Reason: added my observation
# 2  
Old 03-21-2018
Try "$d".
# 3  
Old 03-21-2018
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 03-22-2018
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
#

# 5  
Old 03-22-2018
You have a leading space in d that doesn't seem to exist in your file.
# 6  
Old 03-22-2018
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; 03-22-2018 at 05:51 AM.. Reason: added tagg for code
# 7  
Old 03-22-2018
Quote:
Originally Posted by RudiC
You have a leading space in d that doesn't seem to exist in your file.
Quote:
Originally Posted by scriptor
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
This User Gave Thanks to apmcd47 For This Post:
scriptor (03-22-2018)
Login to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Similar Threads More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How can I assign awk's variable to shell script's variable? geomarine UNIX for Beginners Questions & Answers 8 05-23-2018 03:08 PM
Shell script to pass the config file lines as variable on the respective called function on a script sadique.manzar Shell Programming and Scripting 6 11-16-2017 07:32 AM
Variable not working in grep from script krish.m Shell Programming and Scripting 7 06-05-2014 06:55 AM
Unable to pass shell script variable to awk command in same shell script Ashunayak Shell Programming and Scripting 2 01-27-2014 01:04 PM
Issue with passing variable to Grep in a shell script racshot65 Shell Programming and Scripting 1 08-25-2011 10:48 AM
Grep in Shell script ap2112 Shell Programming and Scripting 5 01-20-2011 11:38 AM
Passing variable from shell script to python script shashi792 Shell Programming and Scripting 4 12-02-2010 06:20 AM
How to grep sql error in shell script and exit the script? allinshell99 Shell Programming and Scripting 2 05-27-2010 10:06 PM
assign awk's variable to shell script's variable? tiger2000 Shell Programming and Scripting 4 04-13-2010 04:53 PM
Shell script / Grep / Awk to variable and Loop Spoonless Shell Programming and Scripting 0 01-26-2010 09:11 AM
How to return a value of a variable from shell script to perl script srkelect Shell Programming and Scripting 3 11-23-2009 03:59 PM
Shell script grep help shriyer Shell Programming and Scripting 11 08-03-2009 12:35 PM
grep in Shell script Krishnaramjis Shell Programming and Scripting 1 02-19-2008 09:43 PM
Grep results to store in a shell variable jojan Shell Programming and Scripting 3 07-26-2007 01:44 PM
Using Grep in a Shell Script nbvcxzdz Shell Programming and Scripting 8 03-19-2005 05:21 AM
All times are GMT -4. The time now is 05:25 PM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?