I want to fetch the Java version in Linux in a single command.
I am new to awk so I am trying something like
java -version|awk '{print$3}'
But that does not return the version. How would I fetch the 1.6.0_21 from the below Java version output?
java version "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b06)
Java HotSpot(TM) 64-Bit Server VM (build 17.0-b16, mixed mode)
Redirect stderr to stdout.
Get first line
Filter the version number.
java -version 2>&1 | head -n 1 | awk -F '"' '{print $2}'
This is a slight variation, but PJW's solution didn't quite work for me:
java -version 2>&1 | head -n 1 | cut -d'"' -f2
just cut the string on the delimiter " (double quotes) and get the second field.
I'd suggest using grep -i version to make sure you get the right line containing the version string. If you have the environment variable JAVA_OPTIONS set, openjdk will print the java options before printing the version information. This version returns 1.6, 1.7 etc.
java -version 2>&1 | grep -i version | cut -d'"' -f2 | cut -d'.' -f1-2
Since (at least on my linux system) the version string looks like "1.8.0_45":
#!/bin/bash
function checkJavaVers {
for token in $(java -version 2>&1)
do
if [[ $token =~ \"([[:digit:]])\.([[:digit:]])\.(.*)\" ]]
then
export JAVA_MAJOR=${BASH_REMATCH[1]}
export JAVA_MINOR=${BASH_REMATCH[2]}
export JAVA_BUILD=${BASH_REMATCH[3]}
return 0
fi
done
return 1
}
#test
checkJavaVers || { echo "check failed" ; exit; }
echo "$JAVA_MAJOR $JAVA_MINOR $JAVA_BUILD"
~
You can use --version and in that case it's not required to redirect to stdout
java --version | head -1 | cut -f2 -d' '
From java help
-version print product version to the error stream and exit
--version print product version to the output stream and exit
Here's my variation (with thanks/acknowledgements to many answers prior to this). My goal is to produce a "Major Version" in line with JEP 223. Assumption are made; I don't know if it works across the board for all released (or to be released) versions of java.
java -version 2>&1 | fgrep -i version | cut -d'"' -f2 | sed -e 's/^1\./1\%/' -e 's/\..*//' -e 's/%/./'
Version
Results
1.6.0_65
1.6
1.8.0_282
1.8
11.0.10
11
15.0.2
15
This way works for me.
# java -version 2>&1|awk '/version/ {gsub("\"","") ; print $NF}'
1.8.0_171
Getting only the "major" build #:
java -version 2>&1 | head -n 1 | awk -F'["_.]' '{print $3}'
Related
I'm attempting to get the OS name (pretty_name)and version (version_ID) on one line via the /etc/os-release file, in a simple command.
Currently, I have the following:
cat /etc/*-release | egrep "PRETTY_NAME|VERSION_ID" | cut -d = -f 2 | tr -d '"'
Which gives me the following output:
7.9
Red Hat Enterprise Linux
I'm looking to get my output looking like this:
Red Hat Enterprise Linux 7.9
What can I do to get the output the way I want it to be?
Since os-release is in shell format (and provided by your OS vendor, whom you're obliged to trust), you can just source it (which treats the quotes as syntax rather than data, so they don't need to be manually removed).
Amending to follow the advice from #Cyrus to prefer Red Hat's extended metadata over the generic variable names when available (but falling back to the generic names otherwise):
. /etc/os-release
printf '%s %s\n' \
"${REDHAT_SUPPORT_PRODUCT:-$PRETTY_NAME}" \
"${REDHAT_SUPPORT_PRODUCT_VERSION:-$VERSION_ID}"
Note; the order of the contents of /etc/*-release can't be guaranteed. This answer focus on OP's case;
Use tac to reverse the output, then use tr '\n' ' ' to concatenate the lines;
cat /etc/*-release | egrep "PRETTY_NAME|VERSION_ID" | cut -d = -f 2 | tr -d '"' | tac | tr '\n' ' '
# 10 Debian GNU/Linux 10 (buster)
Another option would be to read the second and third line of lsb_release witch has a more consistent output;
lsb_release -as 2>/dev/null | sed -n 2,3p | tac | tr '\n' ' '
# 10 Debian GNU/Linux 10 (buster)
The command below will return the corresponding strings
$ docker-compose exec postgres postgres --version
postgres (PostgreSQL) 10.4 (Debian 10.4-2.pgdg90+1)
I am trying to get the postgresql version but when I tried, the Debian version and other numbers are included like
$ pg_version=$(docker-compose exec postgres postgres --version | sed 's/[^0-9.]//g')
10.410.42.901
I am wondering how to get the 10.4 only
awk -v RS=" " '/^[0-9.]+$/{print; exit}'
grep -oE '[.0-9]+' | head -1
tr ' ' '\n' | grep -oE -m 1 '[.0-9]+'
sed 's|^[^0-9.]*\([0-9.]\+\).*|\1|'
Modify the sed as followed would help,
sed -E 's/.*PostgreSQL[^0-9.]+([0-9.]*).*/\1/'
\1 would only match to the version number right behind "PostgreSQL".
pg_version=$(docker-compose exec postgres postgres -V | grep -oE '[.0-9]+' | head -1)
You could use grep with something like this:
$ grep -oP "PostgreSQL.\s\K.+?(?=\s)"
For example:
$ echo "postgres (PostgreSQL) 10.4 (Debian 10.4-2.pgdg90+1)" | grep -oP "SQL.\s\K.+?(?=\s)"
10.4
The \K can be read as excluding everything to the left *SQL)<space> before it and return only the right part .+?(?=\s) until and space \s is found.
I'm creating bash script and want to check version number of program from a line in a file and use it to make different checks and operations.
The version line in file is looking like this (examples):
Program Version 1.3
or
Program Version 1.3.1
It's on different lines in different versions but always follow the same syntax. How to remove the first part and isolate only the version number in order to put it in a variable?
Using GNU grep with -P for perl-style regEx match, and -o flag to return only the matching pattern.
grep -oP 'Program Version \K[^ ]*' file
1.3.1
To save it in a variable
versionNo="$(grep -oP 'Program Version \K[^ ]*' file)"
printf "%s\n" "$versionNo"
1.3.1
Use perl regEx itself,
perl -lne 'print "$1" if /^Program Version (\d.+)/' file
1.3.1
in variable,
versionNo="$(perl -lne 'print "$1" if /^Program Version (\d.+)/' file)" file
printf "%s\n" "$versionNo"
1.3.1
Using GNU sed
sed -r 's/Program Version ([[:digit:]].+).*/\1/' file
1.3.1
and
versionNo="$(sed -r 's/Program Version ([[:digit:]].+).*/\1/' file)" file
printf "%s\n" "$versionNo"
1.3.1
I guess you obtain that Program Version 1.3.1 for example launching some kind of command. Well, try this:
#!/bin/bash
version=$(yourCommandWhichShowsVersion 2> /dev/null | egrep "^Program Version [0-9]" | awk '{print $3}')
Explanation:
You need to launch the command which show the version
You redirect the output to egrep which search through all lines matching only which starts ^ <- this is to start string, with the desired text Program Version, and this [0-9] is to match one number. If you don't know if the version can be 1.3 or 1.3.1 or 1 that's all you need.
awk is going to "select" the second column (first is "Program version" and second is the version number)
Good luck!
Value=$(cat program.txt | grep Program\ Version\ | sed "s/Program \Version\ //g")
Just finds Program Version then strip it out with sed.
Edit: Sorry misread. Removed version number
I would just use grep and cut for this, since the pattern is fixed:
# find the first occurrence of the line (-m 1) starting with the pattern (^) and
# the third field (cut -f3) is the version
# this makes sure we ignore
# a) multiple occurrences of the pattern, if any
# b) occurrence of "Program Version" anywhere else on a line
# we make no assumption about the format of the version number
version=$(grep -m 1 "^Program Version " program.txt | cut -f3 -d' ')
if [[ -z $version ]]; then
# version not specified
# your exception handler here
fi
Some possible solutions for the problem:
awk awk '/Program Version/{print $3}' file.txt
grep grep -oP "Program Version \K[^ ]*" file.txt
sed sed -n '/Program Version /s///p' file.txt
perl perl -lane 'if (/Program Version/) {print $F[2]}' file.txt
(HTH) Hope This Helps.
Here is what I have used at the end:
Version=`find . -type f -name "filename" -exec grep -h 'Program Version' {} + | awk -F " " '{print $3}'`
I have used find + grep to receive the line with the version that I need and -f in awk to define field separator to be blank space and with that I have separated the version number from the rest of the results. Thank you all for your answers.
rsync --version
gives lots of info but I just want to grab the first line
rsync version 3.1.1
How to do this? I tried to pipe into grep but I can't
There are lots of ways to slice this pie. If you want the whole first line, you can use any of these:
rsync --version | head -n 1
rsync --version | awk NR==1
rsync --version | sed -n 1p
rsync --version | grep '^rsync *version'
If you want just the version number without the rest of the line, that's not much harder, but it depends which part of the line you want. On my Mac, the version line reads rsync version 2.6.9 protocol version 29, and a naïve grab would likely yield the 29 - presumably not what you want. Either of the following will output just the 2.6.9 by itself:
rsync --version | awk 'NR==1 {print $3}'
rsync --version | sed -n '1s/^rsync *version \([0-9.]*\).*$/\1/p'
If you just need to get the first line of output use head command
rsync --version | head -1
To preface this, yes, I have already searched for ways to do this with cut, sed, and tail. However, I have found that none of the traditional ways of obtaining the third line of an output work.
java -version | sed 3p
java -version | tail -1
java -version | cut -d$'\n' -f3
All of these commands simply return the same thing as
java -version
What's going on here? How can I get only the third line?
java -version 2>&1 | tail -1