Hi
I have to find if a pattern is present in a line.
eq
line="dasdasd hello asdasdasd"
Have to find if "hello" is present in the line or not.
Which command to be used?
Thanks
Hi
I have to find if a pattern is present in a line.
eq
line="dasdasd hello asdasdasd"
Have to find if "hello" is present in the line or not.
Which command to be used?
Thanks
Use grep, check the man page.
Regards
Grep will search the full file.I want to search only 1 line.
Can grep be used without giving the file name and just giving the variable which has a line stored in it?
i searched but could not find.
echo "$variable" | grep <something>
ok thanks...
But this will return the whole line.How do we implement in a if condition?
if [ pattern is matching in the variable ]
then
do something
fi
something like this :
var="rama raja roha"
if [ `echo "$var" | awk '$0 ~/rama1/ {print 1}'` ]
then
echo "sucess"
else
echo "failure"
fi
Hello!
Your question(s) could easily be answered by searching the Internet with Google. Google is your friend ![]()
Per forum rules, and the benefit of all users, please search the network and the forums before posting a question.
You can easily search the forums using our internal Google search engine or our advanced internal search engine. You can also search our huge UNIX and Linux database by user generated tags or search the database for unanswered questions and provide an answer.
Thank you.
The UNIX and Linux Forums
===========================================================
Found with our internal Google search engine:
It is always a good idea to use search.
you can also do something like this:
found=$(echo $line| grep '$var')
if [ "$found" ]; then
do something
else
do something else
fi
How to implement it if we have to search patten with a space in between?
If we have to search for "rama raja" in the line "rama raja roha".
This is not working.
grep will handle the spaces.
echo "rama raja roha" | grep "rama raja"
rama raja roha
If your search string has space in it and if you put the search string in double quetes, it will find it for you. And better read the below link for more...