Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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.
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
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.