Find the answer to your Linux question:
Results 1 to 3 of 3
Hi All, Newbie here... I have 100's of individual files in /etc/... folder. I'm using using# grep -A1 "Total Assets" STBDetails* to give me this 2 line output: STBDetails.jsp?ip=10.x.x.x&version=5.1.1.6.R: <td><b>Total ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2012
    Posts
    3

    Question Trying to grep for >0 line in 100's of files


    Hi All,

    Newbie here...

    I have 100's of individual files in /etc/... folder. I'm using using# grep -A1 "Total Assets" STBDetails* to give me this 2 line output:

    STBDetails.jsp?ip=10.x.x.x&version=5.1.1.6.R: <td><b>Total Assets</b></td>
    STBDetails.jsp?ip=10.x.x.x&version=5.1.1.6.R- <td>0</td>

    I need to add onto that grep statement to:

    1) Print only the lines that have 0 in string"<td>0</td>") AND see the "Total Assets" STBDetails* line before it (excluding all records that have greater than>0)

    an example of non-matching output:

    STBDetails.jsp?ip=10.x.x.x&version=5.1.1.6.R: <td><b>Total Assets</b></td>
    STBDetails.jsp?ip=10.x.x.x&version=5.1.1.6.R- <td>68</td>

    matching:

    STBDetails.jsp?ip=10.x.x.x&version=5.1.1.6.R: <td><b>Total Assets</b></td>
    STBDetails.jsp?ip=10.x.x.x&version=5.1.1.6.R- <td>0</td>


    After that, in a separate step:
    2) I need to not print the line before, and including the line which has "<td>0</td>" (excluding all line with 0)

    Hope this makes sense. Thanks

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Hi,

    Can't you just grep for the <td>0</td> and include the line before it, instead of the other way around? e.g.:

    Code:
    grep --color -B1 '<td>0</td>'
    run against your output above, that would yield:

    Code:
    STBDetails.jsp?ip=10.x.x.x&version=5.1.1.6.R: <td><b>Total Assets</b></td>
    STBDetails.jsp?ip=10.x.x.x&version=5.1.1.6.R- <td>0</td>
    if you save that output to a variable, you could just use another grep to get just the line you want. putting that all together:

    Code:
    #!/bin/bash
    for file in $(find . -type f -name 'STBDetails*'); do
      out=$(grep -H -B1 '<td>0</td>' $file)
      [ -z "$out" ] && continue
    
      printf "\nStep 1:\n"
      printf "$out\n"
    
      printf "\nStep 2:\n"
      printf "$out\n"|grep '<td>0</td>'
    done

  3. #3
    Just Joined!
    Join Date
    Sep 2012
    Posts
    3
    Thanks atreyu, good tips. The answer was actually right in front of me, with ...grep "<td>[0]</td>" for all 0's, then grep "<td>[1-9]</td>" for non-0 1 digits, grep "<td>[1-9][0-9]</td>" for 2 digit #'s, etc. Still sort of seems like the long (integer) way around but it worked. I didn't know about color setting or variables, so thanks for those too. Onward and upward.

Posting Permissions

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