Reputation: 24815
I would like to get the right value of the following command as a string without double quotes.
$ grep '^VERSION=' /etc/os-release
VERSION="20.04.3 LTS (Focal Fossa)"
When I pipe it with the following awk
, I don't get the desired output.
$ grep '^VERSION=' /etc/os-release | awk '{print $0}'
VERSION="20.04.3 LTS (Focal Fossa)"
$ grep '^VERSION=' /etc/os-release | awk '{print $1}'
VERSION="20.04.3
$ grep '^VERSION=' /etc/os-release | awk '{print $2}'
LTS
How can I fix that?
Upvotes: 2
Views: 743
Reputation: 7831
Since the file /etc/os-release conforms to a variable assignment in bash or the shell in general (POSIX), sourcing it should do the job.
source /etc/os-release; echo "$VERSION"
Using a subshell just in case one does not want the pollute the current env variables.
( source /etc/os-release; echo "$VERSION" )
Assigning it to a variable.
version=$( source /etc/os-release; echo "$VERSION" )
If the shell you're using does not conform to POSIX.
sh -c '. /etc/os-release; echo "$VERSION"'
See your local man page if available.
man 5 os-release
Upvotes: 0
Reputation: 204638
Assuming that "the right value" you want output is 20.04.3
:
$ awk -F'[" ]' '/^VERSION=/{print $2}' file
20.04.3
or if it's the whole quoted string:
$ awk -F'"' '/^VERSION=/{print $2}' file
20.04.3 LTS (Focal Fossa)
Upvotes: 2
Reputation: 133770
1st solution: With your shown samples, please try following awk
code.
awk 'match($0,/^VERSION="[^"]*/){print substr($0,RSTART+9,RLENGTH-9)' Input_file
Explanation: Simple explanation would be, using match
function of awk
to match starting VERSION=" till next occurrence of "
and then printing the matched part(to get only desired output as per OP's shown samples).
2nd solution: Using GNU grep
with PCRE regex enabled option try following.
grep -oP '^VERSION="\K[^"]*' Input_file
3rd solution: Using awk
's capability to set different field separators and then check conditions accordingly and print values.
awk -F'"' '$1=="VERSION="{print $2}' Input_file
Upvotes: 3
Reputation: 627536
You can use an awk
command like
awk 'match($0, /^VERSION="([^"]*)"/, m) {print m[1]}' /etc/os-release
Here, ^VERSION="([^"]*)"
matches VERSION="
at the start of the string (^
), then captures into Group 1 any zero or more chars other than "
(with ([^"]*)
) and then matches "
. The match is saved in m
where m[1]
holds the Group 1 value.
Or, sed like
sed -n '/^VERSION="\([^"]*\)".*/s//\1/p' /etc/os-release
See an online test:
s='VERSION="20.04.3 LTS (Focal Fossa)"'
awk 'match($0, /^VERSION="([^"]*)"/, m) {print m[1]}' <<< "$s"
sed -n '/^VERSION="\([^"]*\)".*/s//\1/p' <<< "$s"
Here, -n
option suppresses the default line output, /^VERSION="\([^"]*\)".*/
matches a string starting with VERSION="
, then capturing into Group 1 any zero or more chars other than "
, and then matching "
and the rest of the string, and replacing the whole match with the Group 1 value. //
means the previous regex pattern must be used. p
only prints the result of the substition.
Both output 20.04.3 LTS (Focal Fossa)
.
Upvotes: 1
Reputation: 786289
You may use this single awk command:
awk -F= '$1=="VERSION" {gsub(/"/, "", $2); print $2}' /etc/os-release
20.04.3 LTS (Focal Fossa)
Upvotes: 4