- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Search in String using UNIX
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
Forums
Discussions
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
02-08-2006 07:52 PM
02-08-2006 07:52 PM
Consider I wish to search for a pattern in string and not in a file.
How do I do that?
e.g.
$ export ch="new,old,ohio,cal,nc"
$ grep -i "ohio" $ch
But grep doestn work here. What command can i give ?
Please advise.
Thank you.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2006 08:01 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2006 08:02 PM
02-08-2006 08:02 PM
Re: Search in String using UNIX
$ export ch="new,old,ohio,cal,nc"
$ echo $ch |grep -i "ohio"
-Arun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2006 08:10 PM
02-08-2006 08:10 PM
Re: Search in String using UNIX
It worked perfectly.
Have a great day ...
Pankaj.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2006 09:07 PM
02-08-2006 09:07 PM
Re: Search in String using UNIX
$ echo ${ch} | grep -i ohio
it will give it.
--
Muthu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2006 09:09 PM
02-08-2006 09:09 PM
Re: Search in String using UNIX
How about assigning points ?
http://forums1.itrc.hp.com/service/forums/helptips.do?#28
-Arun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2006 09:14 PM
02-08-2006 09:14 PM
Re: Search in String using UNIX
Thanks for your reply. It works fine !
Could you please let me know the significance / usage of {} ?
Also, Please explain me the use of ( {} , \ , awk , ``) in below command:-
find . -name "*.b*" -exec ls -l {} \; | awk '{print $8 " "
$9}' > bi_files.lst
lv_lines=`wc -l bi_files.lst | awk '{print $1}'`;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2006 09:21 PM
02-08-2006 09:21 PM
Re: Search in String using UNIX
Take a look at this tutorial about "awk"
http://www.cs.uu.nl/docs/vakken/st/nawk/nawk_1.html
-Arun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2006 09:51 PM
02-08-2006 09:51 PM
Re: Search in String using UNIX
var="hi bye"
grep $var
will try to grep hi string with bye file and other files. It will say bye file is not found. To avoid that use as,
grep ${var}
which will treate hi bye as search string. It will take \t that is tab as record separator.
Another thing is,
If you echo $1 it will give input argument contents of $1. If you give $10 then $1 contents and 0 appeneded to it. To avoid use always with variable as,
${variable}
--
Muthu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2006 09:54 PM
02-08-2006 09:54 PM
Re: Search in String using UNIX
find . -name "*.b*" -exec ls -l {} \; | awk '{print $8 " "
$9}' > bi_files.lst
lv_lines=`wc -l bi_files.lst | awk '{print $1}'`;
==>
find . -name "*.b*" -exec ls -l {} \;
here,
{} is denoting each filename which fetched from find command and doing ls -l execution with that file.
\; or \+ needed to be appened with each -exec.
awk will get from previous command output and print fields separated by white spaces.
$1 - field 1
$9 - field 9
etc.
` ` to execute shell comamnd and store value.
It is same to $(command)
--
Muthu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2006 09:55 PM
02-08-2006 09:55 PM
Re: Search in String using UNIX
# man ksh
# man find
# man awk
it will give all the things. In beginning, it is harder. Once you are reading will give all related informations. All the best.
--
Muthu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2006 10:14 PM
02-08-2006 10:14 PM
Re: Search in String using UNIX
Consider I wish to take the output of the grepped string from a string to a variable. I am sure it can be taken to a file using '>' but how could this be taken to a variable?
Please advise.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2006 11:15 PM
02-08-2006 11:15 PM
Re: Search in String using UNIX
Well, the outptu a grepped string is that whole string, which you already have in a variable.
But let's say you learned about awk and wanted field 4 if the string matched. The use something like:
# x=$(date)
# y=$(echo $x | awk '/Feb/{print $4}')
# echo $y
06:56:21
You also may want to ask yourself how that string got into the variable in the first place and whether at that time not more processing could have been done to avoid this that.
Aklong the same lines... what are you going to do with that string in the variable once you have it? You may find that AWK and PERL can just do the job right there and then.
hth,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2006 09:57 PM
02-09-2006 09:57 PM
Re: Search in String using UNIX
$ export ch="new,old,ohio,cal,nc"
$ result=$(echo $ch |grep -i "ohio")
to have the esult in teh string result.
HTH,
Art
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2006 10:03 PM
02-09-2006 10:03 PM
Re: Search in String using UNIX
# export ch="new,old,ohio,cal,nc"
# result=$(echo ${ch} | grep -i "ohio")
# echo ${result}
But it is not good always if you are having more lines. It is good to redirect to some files as,
grep -i "string" file > result.log
Or more better use -q option to give only success (0) or failure (anything other than 0)
Collect the status as echo ${?}.
--
Muthu