nested gerp Q
I am trying to learn the in and out of grep. I'm having a hard time learning how to nest grep statements. So far I have this working:
alvin@dittohead:~/bin/yab-WX$ grep "update_time" wunderCol.rss <h2 class="online ">Current Conditions <span class="minor">Station reported <span id="update_time">26 seconds ago</span></span></h2> <span class="station-update">Station reported <span id="update_time"><%= data.last_updated %></span></span> I just want the "26 seconds ago" and before anybody suggests GAWK AWK SED head bed lead I'm just trying to learn grep and how to nest commands to pick something out of a line. Thank you ever so much |
have you tried: grep 'patternA.*patternB'
|
No do tell
|
Quote:
|
Well it's not clear to me which is why I said no do tell. If I could make sense of little snippets like that I probably wouldn't need to ask questions.
|
You would need to pipe the output from your present grep command through a "grep -o" that matches just that portion of the line you want to see:
Code:
grep "update_time" wunderCol.rss | grep -o -E '[0-9]+ (seconds|minutes|hours|days|years|decades|centuries|millenia) ago' |
Quote:
Code:
grep 'patternA.*patternB' And this: Quote:
|
Okay thank you, that helps. This is a real struggle for me but I really want to learn this stuff even if it kills me. I'll play with this and probably have more questions but they should take a day or two
|
TB No this isn't homework I'm trying to figure out conky Scripts and from experience whenever I ask a question instead of getting an answer for what I'm trying to learn somebody says try this instead. GAWK AWK SED maybe a better way to go but I want to learn to grep but I have always struggled with syntax
|
This is really a case of using grep to do a bit more than what it was designed to do. grep is intended for selecting whole lines based on a regular expression and does have the option of showing only that part of the line which matched. Selecting a line based on one expression and then selecting text from that line based on a different expression isn't what it was intended to do, though you can accomplish that with two grep processes connected by a pipe. More powerful utilities like sed and awk do that easily in a single process.
Just because a tool can be coerced into doing something doesn't mean it is the best or even an appropriate tool for the job. The sed language has been shown to be Turing-complete, and I've read that someone with far too much time on his hands wrote a sed script that implements the bc interactive calculator language. No one claims that it's an appropriate way to implement bc. |
What rknichols said :)
Do indeed continue to learn as much as you want to about grep eg have a look at egrep, but do be aware that using the right tool for any given job is usually the recommended approach unless eg it is a homework qn (or an interview), where you really are restricted. HTH PS if you're learning tools like sed/awk/grep, then you really need to learn regexes, in which case I can highly recommend http://regex.info/ |
I agree totally because when I see things like grep -o "city=\"[^\"]*\"" | grep -o "\"[^\"]*\"" | grep -o "[^\"]*"} it is very hard to break down in my head. Really I've just never had any luck with this stuff but I keep trying. 25 years ago I purchased a book "Teach Yourself C++ in 21 days". I couldn't get anything to work. A retired systems analyst offered to look the book over for me and said no wonder you're struggling there are syntax errors in the examples in the book. I remember thinking how am I to teach myself anything when the information is erroneous? The only thing I have come close to understanding on my own is yabasic. Thank you again for your suggestion
|
Quote:
ka9qlq, you'll do fine, but the best advice I can offer is to not limit yourself. You have an INCREDIBLE toolbox available to you now, use them all to your advantage. And bear in mind that there are MULTIPLE ways to do what you're after...all are equally right, and people will argue for a century about which is 'best'. The 'best' solution will be the one you are most comfortable with, and which best suits YOU. Quote:
|
Quote:
Perhaps I should have said BASIC regexes haha All those tools depend on 'matching' to get anything done and simple regexes aren't too bad. For more complex stuff you can (of course) resort to eg Perl (ignoring its rather fine regex capability) and simply stick to various string comparisons/shuffling. After all https://en.wikipedia.org/wiki/There%...e_way_to_do_it :) |
THIS is why I like yabasic
aaa$ = "month\": " gosub Alvin print "At " + mdat$ + "\"; aaa$ = "day\": " gosub Alvin print mdat$ + " "; aaa$ = "hour\": " gosub Alvin print mdat$ + ":"; aaa$ = "min\": " gosub Alvin print mdat$ + " " aaa$ = "condition\":" gosub Alvin print mdat$ + " " aaa$ = "condition\":" gosub Alvin print mdat$ + " " //close #b //close #d close #a end label Alvin while(!eof(#a)) //loop line input #a linein$ // grab a line for processing lmark = instr(linein$,aaa$) //cast the hook if lmark >= 1 then // got a bite lmark2 = (len(linein$) - instr(linein$,":")) //cast the hook zzz$ = right$(linein$, lmark2) // grab the data mdat$ = left$(zzz$, len(zzz$) - 1) // chop off trash lmark = instr(mdat$,"\"") //cast the hook if lmark > 0 then // got a bite lmark2 = (len(mdat$) - lmark)//cast the hook zzz$ = right$(mdat$, lmark2) // grab the data mdat$ = left$(zzz$, len(zzz$) - 1) // Your data Sirs fi break fi // end if wend return No I haven't fixed up any arrays yet but look how simple it is. I'd hate to try this in Perl or Python add my skill level. |
All times are GMT -5. The time now is 10:56 AM. |