How to grep variable in shell script?

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to grep variable in shell script?
# 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:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. Shell Programming and Scripting

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

3. Shell Programming and Scripting

Variable not working in grep from script

Hi, If I hard code a value in the grep it works fine from script, when I use variable it doesn't work. On a seperate note, some lines (during testing) works fine from command line but not from scirpt. #!/bin/bash # Will fetch the (oldest - as ls will sort by name by default)Date in the... (7 Replies)
Discussion started by: krish.m
7 Replies

4. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

5. Shell Programming and Scripting

Issue with passing variable to Grep in a shell script

Hi, I'm trying to check if methods specified in a class have been added to the corrosponding interface. My code below is giving me the following errors: grep: function: No such file or directory grep: import($zipfile): No such file or directory grep: function: No such file or... (1 Reply)
Discussion started by: racshot65
1 Replies

6. Shell Programming and Scripting

How to grep sql error in shell script and exit the script?

I need help in the following script. I want to grep the sql errors insert into the error table and exit the shell script if there is any error, otherwise keep running the scripts. Here is my script #!/bin/csh -f source .orapass set user = $USER set pass = $PASS cd /opt/data/scripts echo... (2 Replies)
Discussion started by: allinshell99
2 Replies

7. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

8. Shell Programming and Scripting

Shell script / Grep / Awk to variable and Loop

Hi, I have a text file with data in that I wish to extract, assign to a variable and process through a loop. Kind of the process that I am after: 1: Grep the text file for the values. Currently using: cat /root/test.txt | grep TESTING= | awk -F"=" '{ a = $2 } {print a}' | sort -u ... (0 Replies)
Discussion started by: Spoonless
0 Replies

9. Shell Programming and Scripting

How to return a value of a variable from shell script to perl script

HI , Is there any way to return a value of variable from shell to perl script. Code: === Perl file my $diff1=system("sh diff.sh"); my $diff2=system("sh diff1.sh"); I need exit status of below commands i.e 0 and 1 respectively. Since in both the cases diff is working so system... (3 Replies)
Discussion started by: srkelect
3 Replies

10. Shell Programming and Scripting

Grep results to store in a shell variable

I have the results of a grep with -n store in a shell variable ie VAR=`grep -n -e 'PATTERN' file` How ever I am missing the line breaks in the variable , How do I store the resualts of grep with many lines in to a variables. I want the varable should be the sawmway as we do the grep grep... (3 Replies)
Discussion started by: jojan
3 Replies
Login or Register to Ask a Question