Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Get lines with grep and a variable

 
Highlighted
Occasional Contributor

Get lines with grep and a variable

Hi ..

HP-UX  B.11.31

i wat to get the current printers used in /var/spool/lp/request , only current, it means July 28

Today is July 28, 2011

 

drwxr-xr-x   2 lp         lp            8192 Jul 21 10:34 printer1
drwxr-xr-x   2 lp         lp            8192 Jul 21 10:34 printer2

drwxr-xr-x   2 lp         lp            8192 Jul 22 10:39 printer3

drwxr-xr-x   2 lp         lp            8192 Jul 23 10:41 printer4

drwxr-xr-x   2 lp         lp            8192 Jul 24 10:44 printer5

drwxr-xr-x   2 lp         lp            8192 Jul 28 10:45 printer6

drwxr-xr-x   2 lp         lp            8192 Jul 28 10:47 printer7

drwxr-xr-x   2 lp         lp            8192 Jul 28 10:54 printer8

 

today is July 28

 

so i am doing this:

$ d=`date | awk '{ print $2" "$3}'`
$ echo $d
Jul 28

 

now i want to get the lines under above path under the restriction they have to contain what the variable "d" contains as follows:

ls -lrt /var/spool/lp/request  | grep $d

 

i am getting below error:

$ ls -lrt /var/spool/lp/request  | grep $d
grep: can't open 28

 

i tried this, anything worked  :(  :

$ ls -lrt | grep $d
grep: can't open 28
$ ls -lrt | grep ${d}
grep: can't open 28
$ ls -lrt | grep "$d"
$ ls -lrt | grep '$d'
$ ls -lrt | grep `$d`
sh: Jul:  not found.
usage: grep [-E|-F] [-c|-l|-q] [-bhinsv
        [-f pattern_file...] [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsv
        -f pattern_file... [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsv
$ ls -lrt | grep "${d}"
$ ls -lrt | grep '${d}'
$ ls -rlt | grep "'$d'"

 

how can i work the variable "$d" after grep commnand to get the lines?

 

thanks in advance.

psalm139

5 REPLIES 5
Highlighted
Trusted Contributor

Re: Get lines with grep and a variable

this works for me:

 

ls -lrt | grep "$d" <--- $d is quoted

 

have you thought about using "find"?

Highlighted
Acclaimed Contributor

Re: Get lines with grep and a variable

Hi:

 

Since your 'd' variable contains a space, you need to do:

 

# ls -lrt /var/spool/lp/request  | grep "$d"

 

...to mach lines containing the string represented by 'd'.

 

Regards!

 

...JRF...

Highlighted
Honored Contributor

Re: Get lines with grep and a variable

You also don't need to use awk to extract what you want from the date. The date command has formating options built in.

 

d=$(date +"%d %d")

ls -ltr | grep "${d}"

--
Jeff Traigle
Highlighted
Occasional Contributor

Re: Get lines with grep and a variable

i worked !! thanks ..

i was doing something wrong, now it is working ...

 

how can i assign you the points??

Highlighted
Acclaimed Contributor

Re: Get lines with grep and a variable


@psalm139 wrote:

i worked !! thanks ..

i was doing something wrong, now it is working ...

 

how can i assign you the points??


Hi:

 

"Points" are now "Kudos".  See both these sections:

 

http://h30499.www3.hp.com/t5/help/faqpage/faq-category-id/solutions#solutions

 

http://h30499.www3.hp.com/t5/help/faqpage/faq-category-id/kudos#kudos

 

Despite what you would thing (viz. that marking a post or posts as "solved" means an automatic kudo, the two are distinct operations.  Please do both for the benefit of later readers.

 

Regards!

 

...JRF...