How to grep on grep with regex? How to match only numbers and dots from a grep?
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.
You'd use anchors to associate your pattern with either the end or start of the string, as two examples. Within the pattern, keep in mind that a dot . stands for any single character so if you are looking for a number with points the symbol will have to be escaped: [0-9\.]+
However, if you are parsing HTML, then regular expressions are generally not so appropriate and you'd be better off with a proper parser. There are a lot of choices. One is xmlstarlet though you do have to specify a namespace with -N to use it:
<yanetut>
I think I met a bug in my bash 5.0.17: it treats a > in the ERE as a redirection attempt. Would this ever make sense??
The work-around is to escape the >
Here I enclosed it in the 'string' that also quotes the "" quotes.
</yanetut>
Last edited by MadeInGermany; 02-06-2024 at 08:45 AM.
yes, sed is exactly as good as grep. Or perl. By the way, if you use perl you might download the page with it too, there is no need to use curl. Using perl you can also have correct html parser instead of regex.
<sed nerds>
A simple partial match cannot be used in sed, because a \1 back-reference only works on the current RE, not on a previous RE command.
And sed doesn't know a lookahead either.
==> You need a substitution of the full line.
Code:
sed -En 's#.*"version-number">([^<]*).*#\1#p'
The leading and trailing .* are there to cover the full line. This is slower than a partial match.
With GNU sed you can do one .* first, and if successful do the other .*
Code:
sed -En 's#.*"version-number">##; T; s#<.*##; p'
Code:
sed -En '\#.*"version-number"># { s###; s#<.*##; p; }'
The s## (empty RE) is a back-reference to the previous match, in fact the only supported back-reference to a previous RE command.
</sed nerds>
curl -s https://packagist.org/packages/drupal/core | sed -z 's/.*"version-number">//;s/<.*//'
no backreference, no difficult regex, or lookahead (or lookbehind), no -E. And also there is no need to use both grep and sed.
And also you can do something similar in awk. And it is still unreliable.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.