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.
- 09-17-2012 #1Just Joined!
- Join Date
- Sep 2012
- Posts
- 3
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
- 09-18-2012 #2Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,663
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.:
run against your output above, that would yield:Code:grep --color -B1 '<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: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>
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
- 09-18-2012 #3Just 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.


Reply With Quote
