Find the answer to your Linux question:
Results 1 to 3 of 3
Hi guys, I have a question about using grep to search a document. Now the grep command I am trying to use is: # fsmedinfo 000023 -l | grep -e ...
  1. #1
    Just Joined!
    Join Date
    Mar 2010
    Location
    Sydney
    Posts
    6

    search with grep using 3 criteria

    Hi guys,

    I have a question about using grep to search a document. Now the grep command I am trying to use is:

    # fsmedinfo 000023 -l | grep -e (1) -e dr_policy -e ((001)) >> text

    What I am trying to do is scan a builtin command with a qantam tape backup drive. This command will display a tape number for example 000023(1) or 000024(2) and then a tape policy either dr_policy or archive_policy and finally if media on the tape has been unlinked then it will display a ((001)) at the start of a file list.

    So what I want to do with my script is to find all the tapes in a predefined range and search for 1st version tapes (1), all tapes in the dr_policy and finally any tape that has unlinked media ((001)). If this is not true for all of my criteria then I don't want grep to output to my text file.

    I have been able to get the above command to work but if it finds a 2nd version tape (2) but there is unlinked media ((001)) and has a Policy of dr_policy then i get that information written to my text file which i don't want.

    Is there a way with grep or should I use another command? I was looking into using awk but have not got very far with it.

    Example Output I want to filter:

    -------------------------------------------------------------------------------
    Media Information Report Wed Mar 31 11:52:33 2010
    Media ID: 000025(1)
    Media Type: LTO
    -------------------------------------------------------------------------------
    Storage Area: VolSub
    Class ID: archive_pc Bytes Used: 869,902,569,216
    Last Accessed: 20-mar-2010 02:17:29 Space Remaining: 0
    Media Status: AVAIL Percent Used: 100.00
    Write Protect: N Suspect Count: 0
    Mark Status: UNMARKED Mount Count: 18
    Medium Location: SLOT/BIN
    Formatted: Y
    Number of Segments: 5809
    External Location: N/A

  2. #2
    Linux Enthusiast meton_magis's Avatar
    Join Date
    Oct 2006
    Location
    arizona
    Posts
    665
    I'm not quite understanding what you're looking at, however, if you are trying to use grep to filter content on 2 different lines, it will never work. that is the number 1 limitation of grep, is that it does not span different lines. Awk is good, however I found that it is better to focus on learning perl. Perl is installed everywhere on unix (almost) and can do everything awk does. And there is 1 official version of perl. Awk you have old awk, new awk, gnu awk, solaris awk, AIX awk, HP-UX Awk ... too confusing.

    I just use perl now, it works pretty well.
    New to the internet, technical forums, or the hacker / open source community??
    Read this to learn good posting habits http://www.catb.org/~esr/faqs/smart-questions.html

    RHCE for RHEL version 5
    RHCT for RHEL version 4

  3. #3
    Just Joined!
    Join Date
    Mar 2010
    Location
    Sydney
    Posts
    6
    Hi Meton_magis

    Thanks for the reply, what I am ultimately wanting to do is use grep if possible to search the pipped output of my fsmedinfo command and if it finds (1) and dr_policy and ((001)) then I want it to output via output redirection to a text file the following:

    Media ID: 000025(1)
    Class ID: dr_policy
    ((001)) 643657547 /user/media/movie.mov
    ((001)) 567746575 /user/media/movie2.mov

    If I was to just grep -i '(1)' using my storage media command fsmedinfo then i would get an output of:

    #Media ID: 000025(1) (if there was a (1) at the end of the Media ID field.)
    #

    As there is no other instance of (1) except in the Media ID field I get my desired output.

    If then i use grep -e '(1)' -e dr_policy and -e '((001))' and all of the following patterns are true then I get the expected output of grep (as above). If for any reason one of the 3 criteria is not true then I still get a result but grep will only output the lines that match what is true out of the 3 criteria i have put in the command.

    This result i don't want. Can't i tell grep that if all 3 patterns are not found in the output of my command to just stop and return an exit status of 1?

    Example:
    #grep -e <CRITERIA 1> AND -e <CRITERIA 2> AND -e CRITERIA 3>

    I hope this helps clarify my scripting dilemma. But I am investigating Perl as well.

Posting Permissions

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