Operating System - HP-UX
1753318 Members
6968 Online
108792 Solutions
New Discussion юеВ

Search and Display only searched string

 
Tapas Jha
Valued Contributor

Search and Display only searched string

Dear All,
I have two questions.
1)How can i display only searched string from files? E.g i have few files, i want to search file contents and if any file having string "Hello" on file contents then Hello needs to be put on display, no the entire line which of hello.

2) I have one shell script which is calling to other shell script. How can i pass environment and user variables value to called shell script?

Regards
TK
Tapas Jha
5 REPLIES 5
Peter Godron
Honored Contributor

Re: Search and Display only searched string

TK,
1. Why ? The whole idea of grep is to find the string and give you a reference on where to find it in the file. If you just want to know :
how often the string appears
use the -c option
whether the string appears
use the -l option

2. It depends on how you call the other script:

$ cat a.sh
#!/usr/bin/sh
test="hello"
export test
. ./b.sh

$ cat b.sh
#!/usr/bin/sh
echo $test

$ ./a.sh
hello
$

Or you can pass as parameter:
$ cat a.sh
#!/usr/bin/sh
test="hello"
./b.sh "$test"

$ cat b.sh
#!/usr/bin/sh
echo $

$ ./a.sh
hello
$

Please note the difference in calling ". "

Please also read:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33 on how to reward any useful answers given to your questions.
SANTOSH S. MHASKAR
Trusted Contributor

Re: Search and Display only searched string

Hi,

1] in ur 1st question, I don't understand what
exactly u want do do by only displaying
searched word 'Hello' and not the entire
string contailing 'Hello' ('*Hello*'). Pl.
elaborate on this.

2] U can export variables in caller script
say script1.sh, then only these variables will
be available in called script say script2.sh
and subsequent called scripts called from script1
eg.

script1.sh
-------------
export var1=xyz
export var2=abc

sh script2.sh
--------------



script2.sh
---------------
echo ${var1}

sh script3.sh
-----------------



script3.sh
--------------
echo ${var2}
--------------

This way u can pass env. and variables to
called script.
Dennis Handly
Acclaimed Contributor

Re: Search and Display only searched string

As Peter says, why would you want to only print the string you were searching??

If you really want to do this and don't want the -c or -l output, you can use:
$ grep -q Hello file1 ... && echo "Hello"

>Peter: Please note the difference in calling ". "

Why did you call this out (sourcing) and then went ahead and exported the variable?
You don't need to do that, nor do you need the #!.
Peter Nikitka
Honored Contributor

Re: Search and Display only searched string

Hi,

1)
if fgrep -q "string" file1 file2 ...
then print "string"
fi

2) More than one possibility:
- You can export the variables in scripta and use them in scriptb ...:
scripta:
export var1=value1
export var2=value2
...
scriptb

... and in scriptb just use that varible, but check the varibles first:
[ -n "$var1" ] || print -u2 var1 empty
...
- a 'short hand' export depends on the way you set your variables; this is possible as well in scripta:
var1=value1 var2=value2 scriptb

- you can provide the variables as parameters to scriptb:
scriptb var1=value1 var2=value2

and in scriptb:
#!/usr/bin/sh
eval $@
print $var1
print $var2
...

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Rasheed Tamton
Honored Contributor

Re: Search and Display only searched string

Dear Tapas,

grep -F -x string filename
will print the exact string if the string is a single line.

If it is a mulit word line (line with more than a word); then the grep will print the whole line with the string.

---
Local and global variables are both defined using the syntax

VARIABLE="Some string"

VAR=14

By default these variables are local. To make them global (so that child processes will inherit them) use the command

export VARIABLE

This adds the variable to the process environment.

Regards,
Rasheed Tamton.