LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   nested gerp Q (https://www.linuxquestions.org/questions/linux-software-2/nested-gerp-q-4175534190/)

ka9qlq 02-16-2015 09:05 AM

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

pan64 02-16-2015 09:20 AM

have you tried: grep 'patternA.*patternB'

ka9qlq 02-16-2015 09:21 AM

No do tell

TB0ne 02-16-2015 10:30 AM

Quote:

Originally Posted by ka9qlq (Post 5318092)
No do tell

Again??? You were told...the answer is in post #2, very clearly, and were given the syntax on what to put in, and how. Replace "patternA" and "patternB" with whatever is AROUND the part you want to return.

ka9qlq 02-16-2015 10:48 AM

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.

rknichols 02-16-2015 10:52 AM

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'
That still won't match, "A long time ago in a galaxy far, far away," but you can extend as needed.

TB0ne 02-16-2015 11:35 AM

Quote:

Originally Posted by ka9qlq (Post 5318137)
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 need to think about the 'little snippets' that you read, which is how you will learn.
Code:

grep 'patternA.*patternB'
is VERY clear. You want to get the "26 seconds" part, right???? So something BEFORE that is "patternA", and something AFTER that is "patternB". You pick things that are static, so you'll get reproducible results. In the example you posted, that would be "<span id="update_time">" for A, and "</span>" for B.

And this:
Quote:

Originally Posted by ka9qlq
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.

...says a good bit too. You say you want to learn, and were given an answer, but don't seem to put any thought into what you were given, or do any research on your own as to what it means. And saying you don't want anything awk/gawk/sarcastic-comment related indicates that this may be a verbatim homework question, since other solutions are far easier.

ka9qlq 02-16-2015 01:02 PM

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

ka9qlq 02-16-2015 01:46 PM

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

rknichols 02-16-2015 02:12 PM

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.

chrism01 02-18-2015 04:03 AM

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/

ka9qlq 02-18-2015 11:04 AM

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

TB0ne 02-18-2015 02:10 PM

Quote:

Originally Posted by chrism01 (Post 5319276)
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.

Exactly..that's what led me to ask if it was a homework question, since the only reason to be LIMITED to grep only, would be requirements such as the ones you listed.

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:

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/
Sorry, but regex's still make me want to jump out a window, even after all these years. :)

chrism01 02-19-2015 04:02 AM

Quote:

Sorry, but regex's still make me want to jump out a window, even after all these years
:)

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 :)

ka9qlq 02-19-2015 11:22 AM

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.