Using $variables in grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using $variables in grep
# 1  
Old 10-18-2014
Using $variables in grep

Hi, I'm currently trying to use variables in grep on my script. Printing the variable via echo works fine. Also, if I hard coded the date of the appointment it works just fine. But, if I try to use the $DATE as an argument in grep it doesn't do anything.

Code:
#!/bin/bash

DATE=${2}/${3}/${1}     #take in arguments such as ./appoint.sh 2015 01 25

   echo "Looking for appointments on $DATE ...."  # The $DATE prints in format 01/25/2015
   grep -r "$DATE" *


Can anyone help me?

Last edited by nuclearpenguin; 10-18-2014 at 09:08 PM..
# 2  
Old 10-18-2014
Try putting quotes around the values for DATE.

Looks fine to mine.
Are you sure the files contains this specific format?

AFAIK: 'Official' file extension for bash scripts is .sh.
This User Gave Thanks to sea For This Post:
# 3  
Old 10-18-2014
I tried putting quotes around the values of $DATE, but still doesn't seem to do anything.

Also, yes, I believe the format is correct. The string I'm trying to look for that is contained within the files is formatted as "01/25/2015"

If I try
Code:
grep -r "01/25/2015" *

it works just fine, but with $DATE it doesn't seem to work.
# 4  
Old 10-18-2014
Let's debug it. Could you add the following to it and afterward post the result as it shows in your screen?
Code:
#!/bin/bash
set -x

DATE=${2}/${3}/${1}     #take in arguments such as ./appoint.bash 2015 01 25

   echo "Looking for appointments on $DATE ...."  # The $DATE prints in format 01/25/2015

printf "Where am I before executing grep?: %s\n" $PWD
   grep -r "$DATE" *

# 5  
Old 10-18-2014
Ok I ran the script with
Code:
 $ ./appoint.sh 2015 25 01

and got
Quote:
+ DATE=01/25/2015
+ echo 'Looking for appointments on 01/25/2015 ....'
Looking for appointments on 01/25/2015 ....
+ printf 'Where am I before executing grep?: %s\n' /home/nukep/practice/Files/MailArchive
Where am I before executing grep?:
/home/nukep/practice/Files/MailArchive
+ grep -r 01/25/2015 001 002 003 004 005
# 6  
Old 10-18-2014
Code:
+ grep -r 01/25/2015 001 002 003 004 005

The code shows that it is executing as expected.
grep is searching for the string 01/25/2015 in files: 001, 002, 003, 004 and 005. If that's correct, it is not finding any lines with that 01/25/2015 pattern.

You can verify and see if you find any 01/25/2015 in 001, 002, 003, 004 or 005, manually without grep.

Last edited by Aia; 10-18-2014 at 09:38 PM.. Reason: more
This User Gave Thanks to Aia For This Post:
# 7  
Old 10-18-2014
Oh, I see.
I was inputting the arguments wrong. Instead of writing the year month day, I was writing the year day month. It's working now.
Thanks for the help everyone!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Grep, Variables, IF statements... Fun

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: The issue I am having is part of a validation problem. My script will validate 3 or 4 parameters entered by the... (4 Replies)
Discussion started by: JonLaberge
4 Replies

2. Shell Programming and Scripting

[Help] Grep, Variables, IF statements... Fun

Hello, this will be my first post. I've been browsing around for a bit and have found a lot of useful information on here, hopefully a solution can be provided to me. Issue: Alright, I am looking to search for strings within a file using variables. I have a script that will accept 3 or 4... (2 Replies)
Discussion started by: JonLaberge
2 Replies

3. Shell Programming and Scripting

Using awk or grep with Variables in a script

Good day Geeks, Am having an issue with using variables in a rather simple script, the script is as follows: #!/bin/bash ### Script written by Adigun Gbenga ### Date: April 28, 2012 array=( 1 2 3 4 5 29 7 8 9... (6 Replies)
Discussion started by: infinitydon
6 Replies

4. Shell Programming and Scripting

grep and variables ?

I would like to know if grep can extract the following requirement. I have the folllowing piece of SQL in a file and need to grep the FROM part. mp db-ter-fast-export C100_Input_Target_Table__table_ "${DB}"'/teradata.dbc' -select 'SELECT UPPER(trim(proj_id)) as proj_id, latest_job_id,... (9 Replies)
Discussion started by: anduzzi
9 Replies

5. Shell Programming and Scripting

Print lines where variables occur more than once using grep

Hello, I want to only print lines where variables occur more than once using grep. For eg: Input: $this is a comment int a,b,c,b; int b,c; int d,e; int f,g,f; x=y+5; For the above input, the output would be int a,b,c,b; int f,g,f; I have done grep... (3 Replies)
Discussion started by: prasanna1157
3 Replies

6. Shell Programming and Scripting

grep variables nested within grep

Help, I have a ksh script that has some variables within the grep command, I am then setting another variable that greps the variables that have greps within them. cat $WORKINGDIR/meter_list.txt | while read meter do serialnum=$(cat masterlogfile.txt | grep "$meter" | awk '{ print $19 }'... (7 Replies)
Discussion started by: srichard9
7 Replies

7. Programming

Grep with C variables.

Hi all, I have a problem, I need a grep -v but, the patterns of the grep should be C variables. Example: char var1="h"; char var2="o"; char var3="d"; system("grep -v \"var1 var2 var3\" file.txt"); ---- ---- ---- I try it but... Can u help me? Thanks beforehand P.S. Sorry for... (4 Replies)
Discussion started by: AbelBs
4 Replies

8. Shell Programming and Scripting

How to use variables/array in grep command

Hi, I have a reqmt as i have some values in array and I want to search each value in a file by grep command. Here goes my scripting: #!/bin/ksh set -A ArrayA CENTER LEFT RIGHT echo "ArrayA contains: ${ArrayAİ*¨}" grep -e "${ArrayAİ*¨}" filename.txt The above grep is working for... (4 Replies)
Discussion started by: prashant43
4 Replies

9. Shell Programming and Scripting

awk as grep with variables

i have such awk working fine but how to use variable instead of strings awk '/asdasd.*asda.*asdasd/' file2.txt This is not working: awk '/${a}.*${b}.*${c}/' file2.txt Thanks & regards Peter (7 Replies)
Discussion started by: pp56825
7 Replies

10. Shell Programming and Scripting

Using grep with variables

Being new to shell scripting I hope this question isn't too elementary but here goes. I run a grep statement - grep 'sqr' sqrmodule.par and the grep statement returns correctly the information that I'm looking for. Now I want to take the output from the grep statement and load it into a... (2 Replies)
Discussion started by: gettingstarted
2 Replies
Login or Register to Ask a Question