Operating System - HP-UX
1849239 Members
2805 Online
104042 Solutions
New Discussion

Re: Search in String using UNIX

 
SOLVED
Go to solution
panchpan
Regular Advisor

Search in String using UNIX

Hello.

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.
14 REPLIES 14
R Berwald
Advisor
Solution

Re: Search in String using UNIX

echo $ch | grep -i "ohio"
Arunvijai_4
Honored Contributor

Re: Search in String using UNIX

Hello,

$ export ch="new,old,ohio,cal,nc"

$ echo $ch |grep -i "ohio"

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
panchpan
Regular Advisor

Re: Search in String using UNIX

Thanks a lot !
It worked perfectly.

Have a great day ...
Pankaj.
Muthukumar_5
Honored Contributor

Re: Search in String using UNIX

It is trying to see files with new,old,ohio,cal,nc. You have to print the string and then search with it.

$ echo ${ch} | grep -i ohio

it will give it.

--
Muthu
Easy to suggest when don't know about the problem!
Arunvijai_4
Honored Contributor

Re: Search in String using UNIX

Pankaj,

How about assigning points ?
http://forums1.itrc.hp.com/service/forums/helptips.do?#28


-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
panchpan
Regular Advisor

Re: Search in String using UNIX

Hi Muthu,
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}'`;
Arunvijai_4
Honored Contributor

Re: Search in String using UNIX

Hi Pankaj,

Take a look at this tutorial about "awk"

http://www.cs.uu.nl/docs/vakken/st/nawk/nawk_1.html

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Muthukumar_5
Honored Contributor

Re: Search in String using UNIX

{} has a specific advantage in using with shell variable as,

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


Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Search in String using UNIX

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}'`;

==>

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
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Search in String using UNIX

Read these things in your machine:

# 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
Easy to suggest when don't know about the problem!
panchpan
Regular Advisor

Re: Search in String using UNIX

Hello.
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.
Hein van den Heuvel
Honored Contributor

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.


Arturo Galbiati
Esteemed Contributor

Re: Search in String using UNIX

Hello,

$ export ch="new,old,ohio,cal,nc"

$ result=$(echo $ch |grep -i "ohio")

to have the esult in teh string result.

HTH,
Art

Muthukumar_5
Honored Contributor

Re: Search in String using UNIX

It is simple as,

# 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
Easy to suggest when don't know about the problem!