I have a workable solution but it is not presentable/clean for public usage. The file "version.txt" is both remote and local. The difference is the number:
Remote:
17 March 2022
FVWM myExtensions ver. 3.1.4
Local:
15 March 2022
FVWM myExtensions ver. 3.1.1
In my "poor" solution I manually changed the lines into one line for awk to find the last column and sed removing the dots between the number. Both results are made as variables.
awk '{print $NF}' download/version.txt > tmpGit.txt
VARgit=`sed 's|[.]||g' tmpGit.txt`
awk '{print $NF}' ~/.fvwm/version.txt > tmpLocal.txt
VARlocal=`sed 's|[.]||g' tmpLocal.txt`
if [ "$VARgit" -gt "$VARlocal" ]; then
echo "New update available.";
else
echo "No update.";
fi
I have not found a solution for finding the number in text lines and comparing multiple dot numbers. Thank you in advance.
CodePudding user response:
You could do this with grep
, e.g.:
IFS=. read rmajor rminor rpatch < <(grep -oE '[0-9] \.[0-9] \.[0-9] ' remote.txt)
IFS=. read lmajor lminor lpatch < <(grep -oE '[0-9] \.[0-9] \.[0-9] ' local.txt)
[ $rmajor -gt $lmajor ] && echo "New major version"
[ $rminor -gt $lminor ] && echo "New minor version"
[ $rpatch -gt $lpatch ] && echo "New patchlevel"
CodePudding user response:
finding the number in text line
This is easy to do using regular expression, for example using GNU AWK
, let file.txt
content be
17 March 2022
FVWM myExtensions ver. 3.1.4
then
awk 'BEGIN{FPAT="[0-9.] [.][0-9.] "}NF{print $1}' file.txt
output
3.1.4
Explanation: I inform GNU AWK
that field consting 1 or more of digits or dot, literal dot (hence [.]
rather than .
), 1 or more of digits or dot. Then if one or more such field is present in given line do print 1st field (this solution assume you have at most 1 such field in each line)
(tested in gawk 4.2.1)
comparing multiple dot numbers
I do not know ready solution for this, but I want to note that this task might seems easy, but it is not unless you can enforce certain restriction. In your example you have 3.1.1
and 3.1.4
that is all elements are <10
so you will get expected result if you do comparison as for needs for alphabetic ordering. Consider what happens if you have 3.9
and 3.11
- latter will be consider earlier as first difference is at 3rd position and 1
is earlier in alphabet that 9
. This problem is not present if you might enforce that all parts are consisting of exactly one digit.
CodePudding user response:
Using GNU sort for Version-sort:
$ awk -v OFS='\t' '/FVWM/{print $NF, FILENAME}' local remote | sort -k1,1Vr
3.1.4 remote
3.1.1 local
That tells you the version number in decreasing order and the name of the file containing each version number.
The above was run on these input files:
$ head local remote
==> local <==
15 March 2022
FVWM myExtensions ver. 3.1.1
==> remote <==
17 March 2022
FVWM myExtensions ver. 3.1.4
Given the above this will print the name of the file that contains the highest version number IF the 2 are different and print nothing otherwise:
$ awk -v OFS='\t' '/FVWM/{print $NF, FILENAME}' local remote |
sort -k1,1Vr |
awk '
{ vers[NR]=$1; files[NR]=$0; sub(/[^\t] \t/,"",files[NR]) }
END{ if ( vers[1] != vers[2] ) print files[1] }
'
remote
so then you can just test if that is the remote file name and if so download.