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.
Related
How to make such a double query ?
I try grep -e Pap -e -v Aig filename.txt and grep 'Pap' && -v 'Aig' filename.txt
but both don't work
Use a pipe between your filters
grep -e Pap filename.txt | grep -v Aig
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
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}'
Is there any way to specify a field delimiter for more spaces with the cut command? (like " "+) ?
For example: In the following string, I like to reach value '3744', what field delimiter I should say?
$ps axu | grep jboss
jboss 2574 0.0 0.0 3744 1092 ? S Aug17 0:00 /bin/sh /usr/java/jboss/bin/run.sh -c example.com -b 0.0.0.0
cut -d' ' is not what I want, for it's only for one single space.
awk is not what I am looking for either, but how to do with 'cut'?
thanks.
Actually awk is exactly the tool you should be looking into:
ps axu | grep '[j]boss' | awk '{print $5}'
or you can ditch the grep altogether since awk knows about regular expressions:
ps axu | awk '/[j]boss/ {print $5}'
But if, for some bizarre reason, you really can't use awk, there are other simpler things you can do, like collapse all whitespace to a single space first:
ps axu | grep '[j]boss' | sed 's/\s\s*/ /g' | cut -d' ' -f5
That grep trick, by the way, is a neat way to only get the jboss processes and not the grep jboss one (ditto for the awk variant as well).
The grep process will have a literal grep [j]boss in its process command so will not be caught by the grep itself, which is looking for the character class [j] followed by boss.
This is a nifty way to avoid the | grep xyz | grep -v grep paradigm that some people use.
awk version is probably the best way to go, but you can also use cut if you firstly squeeze the repeats with tr:
ps axu | grep jbos[s] | tr -s ' ' | cut -d' ' -f5
# ^^^^^^^^^^^^ ^^^^^^^^^ ^^^^^^^^^^^^^
# | | |
# | | get 5th field
# | |
# | squeeze spaces
# |
# avoid grep itself to appear in the list
I like to use the tr -s command for this
ps aux | tr -s [:blank:] | cut -d' ' -f3
This squeezes all white spaces down to 1 space. This way telling cut to use a space as a delimiter is honored as expected.
I am going to nominate tr -s [:blank:] as the best answer.
Why do we want to use cut? It has the magic command that says "we want the third field and every field after it, omitting the first two fields"
cat log | tr -s [:blank:] |cut -d' ' -f 3-
I do not believe there is an equivalent command for awk or perl split where we do not know how many fields there will be, ie out put the 3rd field through field X.
Shorter/simpler solution: use cuts (cut on steroids I wrote)
ps axu | grep '[j]boss' | cuts 4
Note that cuts field indexes are zero-based so 5th field is specified as 4
http://arielf.github.io/cuts/
And even shorter (not using cut at all) is:
pgrep jboss
One way around this is to go:
$ps axu | grep jboss | sed 's/\s\+/ /g' | cut -d' ' -f3
to replace multiple consecutive spaces with a single one.
Personally, I tend to use awk for jobs like this. For example:
ps axu| grep jboss | grep -v grep | awk '{print $5}'
As an alternative, there is always perl:
ps aux | perl -lane 'print $F[3]'
Or, if you want to get all fields starting at field #3 (as stated in one of the answers above):
ps aux | perl -lane 'print #F[3 .. scalar #F]'
If you want to pick columns from a ps output, any reason to not use -o?
e.g.
ps ax -o pid,vsz
ps ax -o pid,cmd
Minimum column width allocated, no padding, only single space field separator.
ps ax --no-headers -o pid:1,vsz:1,cmd
3443 24600 -bash
8419 0 [xfsalloc]
8420 0 [xfs_mru_cache]
8602 489316 /usr/sbin/apache2 -k start
12821 497240 /usr/sbin/apache2 -k start
12824 497132 /usr/sbin/apache2 -k start
Pid and vsz given 10 char width, 1 space field separator.
ps ax --no-headers -o pid:10,vsz:10,cmd
3443 24600 -bash
8419 0 [xfsalloc]
8420 0 [xfs_mru_cache]
8602 489316 /usr/sbin/apache2 -k start
12821 497240 /usr/sbin/apache2 -k start
12824 497132 /usr/sbin/apache2 -k start
Used in a script:-
oldpid=12824
echo "PID: ${oldpid}"
echo "Command: $(ps -ho cmd ${oldpid})"
Another way if you must use cut command
ps axu | grep [j]boss |awk '$1=$1'|cut -d' ' -f5
In Solaris, replace awk with nawk or /usr/xpg4/bin/awk
I still like the way Perl handles fields with white space.
First field is $F[0].
$ ps axu | grep dbus | perl -lane 'print $F[4]'
My approach is to store the PID to a file in /tmp, and to find the right process using the -S option for ssh. That might be a misuse but works for me.
#!/bin/bash
TARGET_REDIS=${1:-redis.someserver.com}
PROXY="proxy.somewhere.com"
LOCAL_PORT=${2:-6379}
if [ "$1" == "stop" ] ; then
kill `cat /tmp/sshTunel${LOCAL_PORT}-pid`
exit
fi
set -x
ssh -f -i ~/.ssh/aws.pem centos#$PROXY -L $LOCAL_PORT:$TARGET_REDIS:6379 -N -S /tmp/sshTunel$LOCAL_PORT ## AWS DocService dev, DNS alias
# SSH_PID=$! ## Only works with &
SSH_PID=`ps aux | grep sshTunel${LOCAL_PORT} | grep -v grep | awk '{print $2}'`
echo $SSH_PID > /tmp/sshTunel${LOCAL_PORT}-pid
Better approach might be to query for the SSH_PID right before killing it, since the file might be stale and it would kill a wrong process.