LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 02-09-2010, 04:06 PM   #1
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,270

Rep: Reputation: 99
Question grep a string with space


Hi All,

I am using Sun 5.9.

I am putting the value "Feb 10" in variable 'c' here.

Code:
-bash-3.00$ c=`date | awk '{print $2 " " $3}'`
-bash-3.00$ echo $c
Feb 10
But when I am grep-ing value of 'c', I am getting this error.
Code:
-bash-3.00$ ls -lrth | grep $c
grep: can't open 10
This is because there is a space between "Feb" and "10".

Any other way out ?
 
Old 02-09-2010, 04:13 PM   #2
mjones490
Member
 
Registered: Sep 2005
Distribution: LFS
Posts: 60

Rep: Reputation: 22
Put the $c in single quotes:

Code:
-bash-3.00$ ls -lrth | grep '$c'
 
Old 02-09-2010, 04:16 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
I would have said double-quotes (to allow the variable to be expanded)
 
Old 02-09-2010, 04:18 PM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,154

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
Read the "quoting" section of the bash manual. I don't use a Sun system (nor the Sun OS), but - if the have a POSIX bash - try grep "$c" or grep /\'$c\'/.
 
Old 02-09-2010, 05:17 PM   #5
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,270

Original Poster
Rep: Reputation: 99
Thumbs up

Thanks pixellany and PTrenholme, double quotes is working fine.
I was trying awk/sed Sometimes things are too simple and you try too hard.
 
Old 02-09-2010, 05:22 PM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Another clue: you can simplify the assignment using the date format (no need to parse with awk):
Code:
c=`date +%b\ %e`
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
grep a particular string john83reuben Linux - Newbie 3 12-07-2009 10:34 AM
How to grep a string from all the files under / thomas2004ch Linux - Newbie 23 09-16-2009 09:16 PM
grep string with space (2 word string) casperdaghost Linux - Newbie 7 08-24-2009 03:11 AM
How to grep for a string in a file kaprasanna Linux - Newbie 3 01-06-2009 07:29 AM
grep the exact string only ZAMO Linux - General 11 08-28-2008 06:08 AM


All times are GMT -5. The time now is 04:28 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration