Bob
Bob

Reputation: 33

Check line contains a version number

I have a .txt file that has multiple lines containing only 1 word each. Some of these lines contain version numbers, e.g. 1.2.3.4. Other lines contain other information such as file names (which don't use numbers).

How can I check the last 4 lines of this particular file, and check which lines contain a version number? I need the output to just be a version number (the last 4 lines should be 3 file names and only 1 version number, so the result will always be 1 version number regardless).

Thanks!

Upvotes: 0

Views: 90

Answers (2)

Zumo de Vidrio
Zumo de Vidrio

Reputation: 2091

You can get the latest 4 lines with tail:

tail -4 filename.txt

And since in your case you will only have one line with numbers and the rest of them with won't have numbers, then you may get only the lines which start by a number with grep:

tail -4 filename.txt | grep "^[0-9]"

Upvotes: 2

clt60
clt60

Reputation: 63902

I would use

vn=$(tail -n 4 "$file" | grep -oP '\d+(\.\d+)*')

it will select the version number from the last 4 lines (even if it contains some other characers, like:

some 1.2.3.4 some

Upvotes: 0

Related Questions