Reputation: 1
Withing a directory I have multiple files that have multiple version numbers within the files. I am grepping each file within the directory for these version numbers, sorting them in order to get the most recent version number, and then piping that into 'tail -1' to only the most recent version number and not every grep result.
The data looks something like this:
file1: asdf garbage 1.2.4.1 garbagetext asdf
file2: fsdaf garbage asdfsda 4.3.2.10 fdsaf
and so on. I have already accomplished extracting the most recent version number. I did this with the following:
grep -o '[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}' * | sort | tail -1
The next part is what I am having trouble on. I am trying to extract the number (whether it be one number character or two number characters) before the first period and return that result. Then, I am assuming with a slightly different command do the same thing but for the number after the first period. And again for the number after the second period and finally after the third period.
I have little to no experience with sed or awk, but after a little research I believe either one of these tools are the way to accomplish this.
Thank you!
Edit: Alright I got it, but I am certain this can be done in a much easier way. What I have is the following:
grep -o '[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}' * | sort | tail -1 | grep -o '[0-9]\{1,\}' | sed -n 2p
or sed -n 1p, 3p, 4p depending on which value I want.
Upvotes: 0
Views: 178
Reputation: 247230
To extract a version number, without having to know how many dots are in it, I would use
grep -o '[0-9.]\+' filename | sort --version-sort | tail -1
(assuming you have GNU sort, with the --version-sort
option)
To get just the major version number, pipe the above into one of
sed 's/\..*//'
while read line; do echo ${line%%.*}; done
Upvotes: 0
Reputation: 195289
to get the lastest version number:
grep -P -o "\d+\.\d+\.\d+\.\d+" * |sed 's/.*://g'|awk -F'.' '{v[$0]=($1"."$2$3$4)+0;}END{m=0;for(x in v)if(v[x]>m){m=v[x];n=x;}print n}'
to extract numbers:
kent$ echo "10.2.30.4"|awk -F'.' -v OFS="\n" '$1=$1'
10
2
30
4
you can put the two line together.
Upvotes: 1