Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

bash grep only numbers and compare them

Index.html of the curl command looks like below.

<html>
<head><title>Index of myorg/release/builds/production/</title>
</head>
<body>
<h1>Index of myorg/release/builds/production/</h1>
<pre>Name                                        Last modified      Size</pre><hr/>
<pre><a href="../">../</a>
<a href="1.0.60/">1.0.60/</a>                                      06-Jul-2022 07:47    -
<a href="1.0.63/">1.0.63/</a>                                      06-Jul-2022 10:21    -
<a href="1.0.64/">1.0.64/</a>                                      09-Jul-2022 18:08    -
<a href="1.0.65/">1.0.65/</a>                                      09-Jul-2022 18:42    -
<a href="1.0.71/">1.0.71/</a>                                      10-Jul-2022 10:23    -
<a href="1.0.73/">1.0.73/</a>                                      14-Jul-2022 17:28    -
<a href="1.0.75/">1.0.75/</a>                                      20-Jul-2022 07:25    -
<a href="%f%7D/">{STOCKIO}/</a>                                 24-May-2022 11:09    -
<a href="dashboard-react-module-1.0.29.tar.gz">dashboard-react-module-1.0.29.tar.gz</a>          24-May-2022 07:27  87.74 MB
<a href="dashboard-react-module-1.0.29.tar.gz.md5">dashboard-react-module-1.0.29.tar.gz.md5</a>      24-May-2022 07:27  32 bytes
<a href="dashboard-react-module-1.0.29.tar.gz.sha1">dashboard-react-module-1.0.29.tar.gz.sha1</a>     24-May-2022 07:27  40 bytes
<a href="dashboard-react-module-1.0.29.tar.gz.sha256">dashboard-react-module-1.0.29.tar.gz.sha256</a>   24-May-2022 07:27  64 bytes
<a href="dashboard-react-module.tar.gz">dashboard-react-module.tar.gz</a>                 24-May-2022 07:27  87.74 MB
<a href="dashboard-react-module.tar.gz.md5">dashboard-react-module.tar.gz.md5</a>             24-May-2022 07:27  32 bytes
<a href="dashboard-react-module.tar.gz.sha1">dashboard-react-module.tar.gz.sha1</a>            24-May-2022 07:27  40 bytes
</pre>
<hr/><address style="font-size:small;">Artifactory/6.23.41 Server .myorg.com Port 80</address></body></html>

I’m unable to construct a logic to find the largest entry in the file, here its – 1.0.75

I tried grepping only the numbers like – grep -E "[[:digit:]]\.[[:digit:]]\.[[:digit:]]{1,4}" index.html but it throws the same output as above.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My idea is to get all the numeric entries like 1.0.60, 1.0.63 ... in to an array, cut the last part of the number and compare them to get the largest number, but, unable to find the right grep command that gives only the numeric values.

Or is there a much efficient way to do it ?

>Solution :

Using sed to filter the data, sort to arrange (in case unsorted) and tail to show the last (largest) entry

$ sed -En '/href/s~[^>]*>([0-9][^/]*).*~\1~p' input_file | sort -n | tail -1
1.0.75
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading