- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Search and Display only searched string
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
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
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
тАО04-03-2007 09:42 PM
тАО04-03-2007 09:42 PM
Search and Display only searched string
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-03-2007 09:57 PM
тАО04-03-2007 09:57 PM
Re: Search and Display only searched string
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-03-2007 10:35 PM
тАО04-03-2007 10:35 PM
Re: Search and Display only searched string
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-03-2007 10:38 PM
тАО04-03-2007 10:38 PM
Re: Search and Display only searched string
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 #!.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-03-2007 10:44 PM
тАО04-03-2007 10:44 PM
Re: Search and Display only searched string
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-04-2007 01:05 AM
тАО04-04-2007 01:05 AM
Re: Search and Display only searched string
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.