LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-07-2009, 07:04 AM   #1
brainlesseinstein
LQ Newbie
 
Registered: Jul 2009
Location: Pakistan
Posts: 7

Rep: Reputation: 0
Question Grep variable with space


I want to grep a string variable that is provided by the user as input but it has a space between the two strings

Actually I want user to input a specific date in the format "Aug 4" and then use this variable to list files from a directory for that date.

read -p "Enter Date=" date; //date is in the format Aug 4
cd xyz;
ls -ltr | grep `echo $date` //this does not work
 
Old 08-07-2009, 07:12 AM   #2
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by brainlesseinstein View Post
I want to grep a string variable that is provided by the user as input but it has a space between the two strings

Actually I want user to input a specific date in the format "Aug 4" and then use this variable to list files from a directory for that date.

read -p "Enter Date=" date; //date is in the format Aug 4
cd xyz;
ls -ltr | grep `echo $date` //this does not work
Put double quotes around the "$date":
Code:
read -p "Enter Date=" date; 
cd xyz;
ls -ltr | grep "$date"
I've removed the backticks (which I would have replaced with the $() syntax since I find it easier to read) and the unnecessary call to echo.

Beware, however, of trying to identify files in a directory based on their modification date using this method. On my machine, the output of ls -ltr looks as follows:
Code:
drwxr-x---   9 pwc101 gg 4096 Dec 18  2006 evolution
lrwxrwxrwx   1 pwc101 gg   29 Jan 25  2007 scratch -> /data/geophys/scratch/pwc101/
lrwxrwxrwx   1 pwc101 gg   18 Apr 17  2007 central -> /noc/data/central/
lrwxrwxrwx   1 pwc101 gg   23 Jul  6  2007 pubread -> /noc/itg/pubread/pwc101
drwxr-x---   2 pwc101 gg   72 Nov 21  2007 docs
lrwxrwxrwx   1 pwc101 gg   28 Mar  5  2008 data -> /data/geophys/general/pwc101
drwxr-x---  10 pwc101 gg 4096 Aug 28  2008 src
drwx------   3 pwc101 gg   49 Oct  9  2008 Desktop
drwxr-x---   3 pwc101 gg   19 Nov 23  2008 candy
lrwxrwxrwx   1 pwc101 gg   24 Jan 28  2009 ng -> /noc/gg/general_geophys/
drwxr-x---   7 pwc101 gg 4096 Feb 10 10:25 tmp
lrwxrwxrwx   1 pwc101 gg   30 Mar 25 18:00 ns -> /noc/gg/scratch_geophys/pwc101
drwx------   3 pwc101 gg   30 Mar 26 12:52 mail
drwxr-x---   2 pwc101 gg 4096 Apr 13 09:19 exec
drwxr-x---   8 pwc101 gg   82 May 29 18:11 bin
Thus, if I had entered
Code:
Oct 9
as the answer to your question, I'd get no results from the grep since the actual string in the output of ls is
Code:
Oct  9
You might want to sanitise the output of ls through sed or tr, perhaps, before passing it to grep.

Just a thought

Last edited by pwc101; 08-07-2009 at 07:14 AM.
 
Old 08-09-2009, 02:45 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by brainlesseinstein View Post
I want to grep a string variable that is provided by the user as input but it has a space between the two strings

Actually I want user to input a specific date in the format "Aug 4" and then use this variable to list files from a directory for that date.

read -p "Enter Date=" date; //date is in the format Aug 4
cd xyz;
ls -ltr | grep `echo $date` //this does not work

you can use GNU date for validation of date
Code:
# date -d "ABC 4"
date: invalid date `ABC 4'
# date -d "Aug 4"
Tue Aug  4 00:00:00 SGT 2009
# date -d "4 aug"
Tue Aug  4 00:00:00 SGT 2009
if its invalid, it will produce error. grab that error and prompt user its wrong.
 
Old 08-09-2009, 11:28 PM   #4
brainlesseinstein
LQ Newbie
 
Registered: Jul 2009
Location: Pakistan
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks pwc101.... u were right .. there were two spaces in Aug and 4 ))))))).... it worked.... thanks...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Set MAC as variable with GREP dafuzzbudd Programming 6 05-22-2009 11:07 AM
Redirecting grep output to a variable dimako83 Linux - Software 6 11-17-2008 07:22 AM
Using a variable containing a filename in grep TrumpetMan258 Programming 2 03-01-2008 01:27 PM
Grep result passed to variable cornish Programming 5 11-15-2007 05:05 PM
How to use grep to search for a specific variable.. memo007 Linux - Software 4 02-24-2007 05:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration