LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-16-2015, 09:05 AM   #1
ka9qlq
Member
 
Registered: Feb 2004
Location: Columbus, IN USA
Distribution: Mepis 3.4 rc1
Posts: 119

Rep: Reputation: 26
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
 
Old 02-16-2015, 09:20 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,689

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
have you tried: grep 'patternA.*patternB'
 
Old 02-16-2015, 09:21 AM   #3
ka9qlq
Member
 
Registered: Feb 2004
Location: Columbus, IN USA
Distribution: Mepis 3.4 rc1
Posts: 119

Original Poster
Rep: Reputation: 26
No do tell
 
Old 02-16-2015, 10:30 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,553

Rep: Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946
Quote:
Originally Posted by ka9qlq View Post
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.
 
Old 02-16-2015, 10:48 AM   #5
ka9qlq
Member
 
Registered: Feb 2004
Location: Columbus, IN USA
Distribution: Mepis 3.4 rc1
Posts: 119

Original Poster
Rep: Reputation: 26
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.
 
Old 02-16-2015, 10:52 AM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,770

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
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.
 
1 members found this post helpful.
Old 02-16-2015, 11:35 AM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,553

Rep: Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946
Quote:
Originally Posted by ka9qlq View Post
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.
 
1 members found this post helpful.
Old 02-16-2015, 01:02 PM   #8
ka9qlq
Member
 
Registered: Feb 2004
Location: Columbus, IN USA
Distribution: Mepis 3.4 rc1
Posts: 119

Original Poster
Rep: Reputation: 26
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
 
Old 02-16-2015, 01:46 PM   #9
ka9qlq
Member
 
Registered: Feb 2004
Location: Columbus, IN USA
Distribution: Mepis 3.4 rc1
Posts: 119

Original Poster
Rep: Reputation: 26
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
 
Old 02-16-2015, 02:12 PM   #10
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,770

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
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.
 
1 members found this post helpful.
Old 02-18-2015, 04:03 AM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
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/
 
2 members found this post helpful.
Old 02-18-2015, 11:04 AM   #12
ka9qlq
Member
 
Registered: Feb 2004
Location: Columbus, IN USA
Distribution: Mepis 3.4 rc1
Posts: 119

Original Poster
Rep: Reputation: 26
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
 
1 members found this post helpful.
Old 02-18-2015, 02:10 PM   #13
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,553

Rep: Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946
Quote:
Originally Posted by chrism01 View Post
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.
 
Old 02-19-2015, 04:02 AM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
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
 
Old 02-19-2015, 11:22 AM   #15
ka9qlq
Member
 
Registered: Feb 2004
Location: Columbus, IN USA
Distribution: Mepis 3.4 rc1
Posts: 119

Original Poster
Rep: Reputation: 26
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] gerp and regex , something in between .. a4z Programming 21 09-20-2014 01:40 PM
nested if statement dsmith8890 Programming 3 06-24-2012 10:45 AM
Nested case kasparov29 Programming 4 12-05-2011 12:21 PM
why do we use gerp & env & wht is their funda nikhil_rs_maheshwari Solaris / OpenSolaris 12 03-06-2006 02:21 PM
Starting a nested X (or something like that :-D) oribd Linux - Newbie 2 10-21-2005 07:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration