Find the answer to your Linux question:
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.
  1. #1
    Just 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

  2. #2
    Linux Guru
    Join Date
    Apr 2003
    Location
    London, UK
    Posts
    3,284
    This is the command you need:

    Code:
    grep 'john' test | awk '{print $2}'
    Basically, what this does is grep the file named test for all lines that contain the text "john".

    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

  3. #3
    flw
    flw is offline
    Linux Engineer
    Join Date
    Mar 2003
    Location
    U.S.A.
    Posts
    1,025
    grep 'john' test | awk '{print $2}'
    So if this was changed to
    Code:
    grep 'john' /  | 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?
    Dan

    \"Keep your friends close and your enemies even closer\" from The Art of War by Sun Tzu\"

  4. #4
    Linux Guru
    Join Date
    Apr 2003
    Location
    London, UK
    Posts
    3,284
    You would need to use:
    Code:
    grep 'john' -r /home/jason/  | awk '{print $2}'
    Notice the -r, meaning recursive lookup on the files given in the directory presented and all subdirectories.

    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

  5. #5
    flw
    flw is offline
    Linux 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 J
    Dan

    \"Keep your friends close and your enemies even closer\" from The Art of War by Sun Tzu\"

  6. #6
    flw
    flw is offline
    Linux 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 just
    Code:
    grep 'text string' -r /home > /textstring.txt
    Edit: Yes the above is correct. Thanks for the lead on it J.
    Dan

    \"Keep your friends close and your enemies even closer\" from The Art of War by Sun Tzu\"

  7. #7
    Just Joined!
    Join Date
    Mar 2003
    Posts
    10
    or u can try
    grep john names > found
    read a b < found
    echo $b

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •