- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- search excat string in another string (grep "fails...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-05-2008 01:49 AM
тАО06-05-2008 01:49 AM
i have an statement which i have to correct because it shows the wrong result.
i want to search an excat string in another string, command "grep" shows the wrong result:
example:
STRINGS="string1 string2 string3"
search_string="string"
incorrect:
if [ `echo "${STRINGS}" | grep "${search_string}" > /dev/null 2>/dev/null; \
echo $?` -gt 0 ]
then
echo "not found"
fi
my solution will be a "for statement":
exists="N"
for string in ${STRINGS}
do
if [ "${string}" = "${search_string}" ]
then
exists="J"
break
fi
done
echo $exists
is there any solution with one command. i will use in a shell script, also it possible to use perl for this but my experience in perl is very small ...
regards
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-05-2008 02:45 AM
тАО06-05-2008 02:45 AM
Solutionyou can use le -w flag for grep:
#> STRINGS="string1 string2 string3"
#> search=string
#> echo "${STRINGS}" |grep -w "$search_string"
return nothing
#> search=string2
#> echo "${STRINGS}" |grep -w "$search_string"
string1 string2 string3
hope it'll help you
Regards,
Cerick Gaillard
- Tags:
- grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-05-2008 03:01 AM
тАО06-05-2008 03:01 AM
Re: search excat string in another string (grep "fails")
#!/usr/bin/ksh
STRINGS="string1 string2 string3"
search_string="string"
echo "${STRINGS}" | grep -qw "${search_string}"
if [ $? -ne 0 ]; then
echo "not found"
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-05-2008 03:29 AM
тАО06-05-2008 03:29 AM
Re: search excat string in another string (grep "fails")
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-05-2008 04:17 AM
тАО06-05-2008 04:17 AM
Re: search excat string in another string (grep "fails")
Here's a Perl solution embedded in a shell:
# S="In here or there is his or her string"
# X="her"
# echo ${S} | perl -ne 'BEGIN{$X=shift or die};exit 0 if m{\b$X\b}i;exit 1' $(echo ${X})
# [ $? -eq 0 ] && echo "ok!" || echo "not found"
ok!
Notice that the match for the string ${X} in the string ${S} is a case-insensitive match that is bounded (\b) by non-word characters like the '-w' option of 'grep'.
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-05-2008 05:12 AM
тАО06-05-2008 05:12 AM
Re: search excat string in another string (grep "fails")
A quick addendum after much needed coffee. You certainly don't need the trailing $(echo ${X}) !!! The Perl snippet that emulates 'grep -qw' is simply:
# echo ${S} | perl -ne 'BEGIN{$X=shift or die};exit 0 if m{\b$X\b}i;exit 1' ${X}
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-05-2008 07:05 AM
тАО06-05-2008 07:05 AM
Re: search excat string in another string (grep "fails")
What (would you say) is the "right" result?
Would adding some spaces do what you want?
echo " ${STRINGS} " | grep " ${search_string} "
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-05-2008 10:47 PM
тАО06-05-2008 10:47 PM
Re: search excat string in another string (grep "fails")
Unfortunately this doesn't work at beginning and ending of line or if delimited by punctuation. -w makes this all a no-brainer.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-05-2008 10:57 PM
тАО06-05-2008 10:57 PM
Re: search excat string in another string (grep "fails")
> beginning and ending of line
Look again at where I put those spaces. It
sure seemed to work when I tried it. Did it
fail for you? (Demonstrate?)
> or if delimited by punctuation. -w makes
> this all a no-brainer.
I like whatever works, wherever it works. I
may still have an old grep somewhere which
doesn't do "-w", but I have spaces
everywhere.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-05-2008 11:52 PM
тАО06-05-2008 11:52 PM
Re: search excat string in another string (grep "fails")
I was looking closer at the grep than the echo.
But my comment was more geared to files where there is whitespaces vs spaces, punctuation, etc.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-06-2008 07:39 AM
тАО06-06-2008 07:39 AM
Re: search excat string in another string (grep "fails")
> echo. [...]
And I was looking more closely at the problem
posed by the questioner.
As usual, depending on the details, there's
more than one way to solve a problem like
this. Many things are possible.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-06-2008 08:02 AM
тАО06-06-2008 08:02 AM
Re: search excat string in another string (grep "fails")
> Steven: As usual, depending on the details, there's more than one way to solve a problem like this. Many things are possible.
Indeed that is one reason I suggested a Perl solution: TMTOWTDI
Regards!
...JRF...