I have a text file (mcafee agent install script named install.sh) that contains many lines, including the following lines that I am interested in:
THIS_MAJOR=5
THIS_MINOR=0
THIS_PATCH=6
THIS_BUILD=491
I want to use awk to print the numbers from column 2 on each line as a dot separated version string on one line so that the output looks like this:
5.0.6.491
I am using the following awk command to match on the lines containing the version numbers in column 2, and to print the value and append the dot character with printf:
awk -F "=" '/^THIS_/ && /MAJOR|MINOR|PATCH|BUILD/ && !/AGENT/ {printf("%s.", $2)}' /tmp/install.sh
Which prints out:
5.0.6.491.
I'm trying to avoid printing the last '.' character
Here's what I've tried so far
Using gsub to replace the '.' character.
Not ideal, because I should be able to tell awk to just not print the '.' if it's operating on the last line.
awk -F "=" '/^THIS_/ && /MAJOR|MINOR|PATCH|BUILD/ && !/AGENT/ {printf("%s.", $2)} END{gsub(".","",$2)}' /tmp/install.sh
Attempt to use END and not print the last "." :
awk -F "=" '/^THIS_/ && /MAJOR|MINOR|PATCH|BUILD/ && !/AGENT/ {printf("%s.", $2)} END{printf("$s", $2)}' /tmp/install.sh
Which no longer matches correctly on the lines that contain the version numbers.
Attempt to determine number of lines, and while not processing the last line, print the dot character, and on the last line, print without the dot character.
awk -v nlines=$(wc -l < $a) -F "=" '/^THIS_/ && /MAJOR|MINOR|PATCH|BUILD/ && !/AGENT/ {printf("%s.", $2)} NR != nlines { printf("%s", $2)}' $a >> /tmp/install.sh
Which returns:
-bash: $a: ambiguous redirect
How can I get awk to not print the '.' character for the number from the last line?
Thanks