LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   grep a string with space (https://www.linuxquestions.org/questions/programming-9/grep-a-string-with-space-788077/)

vikas027 02-09-2010 03:06 PM

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 ?

mjones490 02-09-2010 03:13 PM

Put the $c in single quotes:

Code:

-bash-3.00$ ls -lrth | grep '$c'

pixellany 02-09-2010 03:16 PM

I would have said double-quotes (to allow the variable to be expanded)

PTrenholme 02-09-2010 03:18 PM

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\'/.

vikas027 02-09-2010 04:17 PM

Thanks pixellany and PTrenholme, double quotes is working fine.
I was trying awk/sed ;) Sometimes things are too simple and you try too hard.

colucix 02-09-2010 04:22 PM

Another clue: you can simplify the assignment using the date format (no need to parse with awk):
Code:

c=`date +%b\ %e`


All times are GMT -5. The time now is 02:05 PM.