Operating System - Linux
1754150 Members
3266 Online
108811 Solutions
New Discussion юеВ

Re: scanning the contents of a variable

 
SOLVED
Go to solution
Bryan D. Quinn
Respected Contributor

scanning the contents of a variable

Ok, this seems like it should be really easy, but I can not for the life of me figure it out.

What I am trying to do is this: I have a variable which contains a long string like the following.

VAR="Some text. Some more text. Even more text."

What I want to do is scan or grep the contents for a certain set of characters. Just like I was grepping a file for a certain string.

What, if anything, am I missing here?

Any help would be greatly appreciated.

Thanks,
-Bryan
11 REPLIES 11
Rick Garland
Honored Contributor

Re: scanning the contents of a variable

echo $VAR | grep some_pattern

echo the contents the the $VAR then pipe that through grep
Mel Burslan
Honored Contributor
Solution

Re: scanning the contents of a variable

If you want to locate where the substring is located with respect to the start of the string, I am not sure if it can be done via simple ksh but substr() function in perl is what you need.

if you are only concerned that if the string contains the substring you are looking for in a binary manner, i.e. "yes it contains" or "no it doesn't contain"

echo $string | grep -q $substring
r=${?}

if [ $r -eq 0 ]
then
echo "Substring was in the string"
else
echo "String sis not contain any of substring"
fi


hope this helps
________________________________
UNIX because I majored in cryptology...
Leif Halvarsson_2
Honored Contributor

Re: scanning the contents of a variable

Hi,
echo $VAR |grep
echo $VAR |awk ' /
Pete Randall
Outstanding Contributor

Re: scanning the contents of a variable

Bryan,

Not sure what you're looking for in your results - does this do anything for you:

VAR="Some text. Some more text. Even more text."

echo $VAR |grep text


Pete

Pete
Doug O'Leary
Honored Contributor

Re: scanning the contents of a variable

Hey;

My $.02...

echo "${var}" | grep "$text" > /dev/null 2>&1
[[ $? -eq 0 ]] && echo "Text found" || echo "Text NOT FOUND"

HTH;

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Bryan D. Quinn
Respected Contributor

Re: scanning the contents of a variable

Pete,

I did the

echo $VAR | grep 'text'

option and it just returned the entire contents of the variable. As if it only did the echo portion of the command. I will try it again, maybe I fat fingered something.

-Bryan
Mel Burslan
Honored Contributor

Re: scanning the contents of a variable

it is the expected result, i.e., return the whole line contaning the substring you are looking for.

what did you expect to get instead ?
________________________________
UNIX because I majored in cryptology...
RAC_1
Honored Contributor

Re: scanning the contents of a variable

echo ${var} | tr " " "\n" | grep -i 'text'
There is no substitute to HARDWORK
Bryan D. Quinn
Respected Contributor

Re: scanning the contents of a variable

Well heck Mel...you have just showed me the error of my ways!

I guess I was expecting it to output just the text it was grepping for!

D'oh! Thanks to everybody who responded.

Special thanks to Mel and Doug for providing me with a means for getting the result I was looking for. If I would have really thought about what I was doing I would not have had to post in the first place.

Thanks!
-Bryan