Results 1 to 7 of 7
anyone knows how to use the grep command to display only a specific string ina file.
e.g.
if i have a file called name:
Chris 22
John 44
what grep ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 03-25-2003 #1Just Joined!
- Join Date
- Mar 2003
- Posts
- 10
UNIX shell command
anyone knows how to use the grep command to display only a specific string ina file.
e.g.
if i have a file called name:
Chris 22
John 44
what grep command can i use to search for John, but the result only displays 44 instead of John 44???
thanks very much
- 03-26-2003 #2Linux Guru
- Join Date
- Apr 2003
- Location
- London, UK
- Posts
- 3,284
This is the command you need:
Basically, what this does is grep the file named test for all lines that contain the text "john".Code:grep 'john' test | awk '{print $2}'
We then send the output of this command using a pipe " | " directly to awk which in turn output's the second item($2) in each line (eg, 44) to the screen.
Jason
- 03-26-2003 #3Linux Engineer
- Join Date
- Mar 2003
- Location
- U.S.A.
- Posts
- 1,025
So if this was changed togrep 'john' test | awk '{print $2}'would it search for the text string "john" starting from root including subdir's, just look in root only or just fail?Code:grep 'john' / | awk '{print $2}'
Dan
\"Keep your friends close and your enemies even closer\" from The Art of War by Sun Tzu\"
- 03-26-2003 #4Linux Guru
- Join Date
- Apr 2003
- Location
- London, UK
- Posts
- 3,284
You would need to use:
Notice the -r, meaning recursive lookup on the files given in the directory presented and all subdirectories.Code:grep 'john' -r /home/jason/ | awk '{print $2}'
Essentially, the command above looks at all the files and sub-files in /home/jason and below and shows the 2nd entry on a line that contains the word john.
Remember that it will be a bit odd to run the exact command above from the root / directory, as it would put the system under a lot of strain, and you proberly not find what you were looking for.
It is good to use -r when you have a lot of files Pre-Formatted in on way, located in one directory.
Without the -r being used grep would only search the given directory.
Hope that made sense.
Jason
- 03-26-2003 #5Linux Engineer
- Join Date
- Mar 2003
- Location
- U.S.A.
- Posts
- 1,025
The idea of running it from root would be looking for something like a ip for the web proxy in a lan or wan and you need to find out where it is referenced in the various conf files.
In my situation, someone else setup the servers before I ever started so very few things are in the default locations or even files. Redirects everywhere.
So this will come in handy for me. its all so the equilvilant of the search Windoze does from c: for all file searching for text strings.
Thanks JDan
\"Keep your friends close and your enemies even closer\" from The Art of War by Sun Tzu\"
- 03-26-2003 #6Linux Engineer
- Join Date
- Mar 2003
- Location
- U.S.A.
- Posts
- 1,025
Well by searching from / I get "Invalid augment". But works fine if I start it in a sub dir like /usr instead of /.
So if I wanted to redirect it to a new file would I have to touch it first or could I justEdit: Yes the above is correct. Thanks for the lead on it J.Code:grep 'text string' -r /home > /textstring.txt
Dan
\"Keep your friends close and your enemies even closer\" from The Art of War by Sun Tzu\"
- 03-29-2003 #7Just Joined!
- Join Date
- Mar 2003
- Posts
- 10
or u can try
grep john names > found
read a b < found
echo $b


Reply With Quote
