- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Get lines with grep and a variable
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2011 07:57 AM
07-28-2011 07:57 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2011 08:04 AM
07-28-2011 08:04 AM
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"?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2011 08:05 AM
07-28-2011 08:05 AM
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2011 11:36 AM
07-28-2011 11:36 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2011 11:59 AM
07-28-2011 11:59 AM
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??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2011 12:24 PM
07-28-2011 12:24 PM
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...