Operating System - HP-UX
1821187 Members
3253 Online
109631 Solutions
New Discussion юеВ

Problems to use grep when there is a variale with several spaces between spaces ....

 
SOLVED
Go to solution
Manuales
Super Advisor

Problems to use grep when there is a variale with several spaces between spaces ....

Hi ..

${logftp}_out=fileout

i have a file nane which is named "fileout" contains:

200 PORT command successful.
150 Opening ASCII mode data connection for CNA-HOLDINGS-NJ-AR-I.
-C--------FTP B BLABLABLA 215946 15661 Jun 02 14:57 FILE_A
Total number of batches listed: 1

I have this variable in my script:
unixx:user1> dia2=`date +'%b %d'`
unixx:user1> echo $dia2
Jun 02 (there is a space between Jun and 02)

i wat to obtain the line where is the date, i do this one taking the date :

${logftp}_out = fileout

unixx:user1> cat ${logftp}_out | grep $dia2
grep: can't open 02

how can i do to grep takes of part as parameter of dia2 the number 02?

Thanks ...
5 REPLIES 5
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Problems to use grep when there is a variale with several spaces between spaces ....

cat ${logftp}_out | grep "${dia2}"
If it ain't broke, I can fix that.
Sandman!
Honored Contributor

Re: Problems to use grep when there is a variale with several spaces between spaces ....

Manuales,

Change your grep command so that the string you need to search for is presented as a single argument by surrounding it with double-quotes like...

unixx:user1> cat ${logftp}_out | grep "$dia2"

cheers!
Manuales
Super Advisor

Re: Problems to use grep when there is a variale with several spaces between spaces ....

Worked with second sentence ...

and if i have:

dia1=`date "+%b %d"|$ppppp -pe 's/(...)0(\d)/$1 $2/'`

echo "${dia}"
Jun 2 #there is two spaces between Jun and 2"

how can i use grep here?

Thanks ....
Sandman!
Honored Contributor

Re: Problems to use grep when there is a variale with several spaces between spaces ....

same way as before...as long as you surround the variable "dia1" with double-quotes.

# grep "$dia1"

cheers!
Bill Hassell
Honored Contributor

Re: Problems to use grep when there is a variale with several spaces between spaces ....

Understanding the shell will help here. Spaces are the default separators but you want to turn off that feature. That's where using double (or single) quotes will work. The variable contains a string with imbedded spaces but when the shell processes your variable, it is turned into several separate strings. So grep sees the first string as the pattern and the rest the strings are taken as filenames to search.

Using "$somevariable" will tell the shell to treat this as one long string including all spaces. grep will see this long string and will perform the search using the entire string.

The single quote '$somevariable' turns off all special handling for every character so grep will be looking for $somevariable and not the contents of that variable.


Bill Hassell, sysadmin