I have to grep from a file name temp which has something like this
Process State
BE_RP:1 [PL_2_3] Running
BE_RP:2 [PL_2_4] Running
BE_RP:3 [PL_2_5] Running
BE_RP:4 [PL_2_6] Running
FE_SCTP:0 [PL_2_3] Running
FE_SCTP:1 [PL_2_4] Running
BE_NMP:0 Not Running
OAM:0 Running
I need to write a egrep statement which will return the number of process which are in running or not running state.
awk '/^OAM/ { next } /Not Running[ \t]*$/{s++} END {print s, NR-s-1}' foo.txt
Prints <running> <not running>
Running
$ grep -v 'OAM' input | grep -cP '(?<!Not) Running\s*$'
6
Not Running
$ grep -v 'OAM' input | grep -cP 'Not Running\s*$'
1
sed '{
1 d
s/^[^:]*:[0-9]*[ ]*//
s/^[^]]*]//
s/^[ ]*//
}' input_file | sort | uniq -c
grep -P '^(?!OAM:0).*Running' temp | cut -f2 | wc -l
Related
I'm trying to get grep/sed out the following output: "name":"test_backup_1" from the below response
{"backups":[{"name":"test_backup_1","status":"CORRUPTED","creationTime":"2019-11-08T15:03:49.460","id":"test_backup_1"}]}
I have been trying variations of the following grep -Eo 'name:"\w+\"' but no joy.
I'm not sure if it would be easier to achieve this using grep or sed?
The way I am running this is curling a response from the server and saving it to a local variable, then echo out the variable and pipe grep/sed
example of what I am running
echo ${view_backup} | grep -Eo '"name":"\w+\"'
Referencing #sundeep answer
grep -Eo '"name":"[^"]+"'
resulted in the expected output
Make sure to transform the file to one line before grep
and pipe from your curl
echo `curl --silent https://someurl | tr -d '\n' | grep -oP "(?<=name\":\")[^\"]+"`
will return
test_backup_1
If you want more variables you can chain the -oP grep like in this example where I get some data on a danish license plate (bt419329)
curl --silent https://www.tjekbil.dk/api/v2/nummerplade/bt41932 | grep -oP -m 1 "(?<=\"RegNr\":\")[^\"]+|(?<=\"MaerkeTypeNavn\":\")[^\"]+|(?<=\"MaksimumHastighed\":)[^,]+"| tr '\n' ' '
returns
BT41932 SKODA 218
echo "Linux/DEB/mainbinary-0.1.20190424165331-0-armdef.deb" | grep -oE "([^\/]+$)"
This prints just the filename, without the directory structure, but I cannot manage to print just mainbinary from that string. Suggestions?
And a sed alternative to PS.'s great grep -oP
echo "Linux/DEB/mainbinary-0.1.20190424165331-0-armdef.deb" |sed -r 's#^.*/([^-]+).*#\1#'
mainbinary
echo "Linux/DEB/mainbinary-0.1.20190424165331-0-armdef.deb" |grep -oP '.*/\K[^-]+'
mainbinary
This will scan till last / and ignore everything to its left and keep moving until - (excluding)
With any awk in any shell on any UNIX machine:
$ echo "Linux/DEB/mainbinary-0.1.20190424165331-0-armdef.deb" | awk -F'[/-]' '{print $3}'
mainbinary
How to get only the process ID for a specified process name in Linux?
ps -ef|grep java
test 31372 31265 0 13:41 pts/1 00:00:00 grep java
Based on the process id I will write some logic. So how do I get only the process id for a specific process name.
Sample program:
PIDS= ps -ef|grep java
if [ -z "$PIDS" ]; then
echo "nothing"
else
mail test#domain.example
fi
You can pipe your output to awk to print just the PID. For example:
ps -ef | grep nginx | awk '{print $2}'
9439
You can use:
ps -ef | grep '[j]ava'
Or if pgrep is available then better to use:
pgrep -f java
Use this: ps -C <name> -o pid=
This command ignore grep process, and just return PID:
ps -ef | grep -v grep | grep java | awk '{print $2}'
why not just pidof ?
pidof <process_name>
it will return a list of pids matching the process name
https://linux.die.net/man/8/pidof
I am looking to extract a basedir from the output of ps -ef | grep classpath myprog.jar
root 20925 20886 1 17:41 pts/0 00:01:07 /opt/myprog/java/jre/bin -classpath myprog.jar
java is always a sub-dir under the basedir but the install path can vary from server to server e.g.
/usr/local/myprog/java/jre/bin
/opt/test/testing/myprog/java/jre/bin
So once i have my string how do I extract everything from before java until the beginning of the path?
That is, /usr/local/myprog or /opt/test/testing/myprog/
Using sed:
$ echo "root 20925 20886 1 17:41 pts/0 00:01:07 /opt/myprog/java/jre/bin -classpath myprog.jar" | sed 's/.*\ \(.*\)\/java.*/\1/'
/opt/myprog
Using grep -P:
ps -ef | grep -oP '\S+(?=/java)'
/opt/myprog
If your grep doesn't support -P then use:
s='root 20925 20886 1 17:41 pts/0 00:01:07 /opt/myprog/java/jre/bin -classpath myprog.jar'
[[ "$s" =~ (/[^[:blank:]]+)/java ]] && echo "${BASH_REMATCH[1]}"
/opt/myprog
echo "root 20925 20886 1 17:41 pts/0 00:01:07 /opt/myprog/java/jre/bin -classpath myprog.jar" | awk '{split($8,a,"/java"); print a[1]}'
Use pgrep to find all of the Java processes instead of using ps -ef | grep .... This way, you don't have to worry about your grep command showing up as one of your items.
Instead of running ps -ef, you can use the -o option to only pull up the desired fields, and most ps commands take --no-header to eliminate the header fields. This way, your script doesn't have to worry about header lines.
Finally, I am using Shell Parameter Expansion which is sometimes way easier than using sed to change a variable:
$ ps -o pid,args --no-headers $(pgrep -f "java .* myproj.jar") | while read pid command arguments
do
directory=${command%/java*}
echo "The directory for Process ID $pid is $directory"
done
By the way, you could be running multiple commands, so I loop through the ps command.
ps axo args | awk '/classpath myprog.jar/{print substr($0, 0,index($0, "java")-1)}'
For example:
$ echo '/opt/myprog/java/jre/bin -classpath myprog.jar' \
| awk '/classpath myprog.jar/{print substr($0, 0,index($0, "java")-1)}'
/opt/myprog/
You can (and probably should) switch both of the $0's to $1's if you know for sure that your path will not contain spaces. Or add additional fields to the ps -o list using commas (as in, o pid,args) and use $2 rather than $1.
You can match the following regex:
'((\/\w+)+)\/java'
and the first captured group \1 or $1 will contain the wanted string
Demo: http://regex101.com/r/zU2vV4
I've got supervisor's status output, looking like this.
frontend RUNNING pid 16652, uptime 2:11:17
nginx RUNNING pid 16651, uptime 2:11:17
redis RUNNING pid 16607, uptime 2:11:32
I need to extract nginx's PID. I've done it via grep -P command, but on remote machine grep is build without perl regular expression support.
Looks like sed or awk is exactly what I need, but I don't familiar with them.
Please help me to find a way how to do it, thanks in advance.
sed 's/.*pid \([0-9]*\).*/\1/'
Using AWK alone:
awk -F'[ ,]+' '{print $4}' inputfile
$ cat $your_output | sed -s 's/.*pid \([0-9]\+\),.*/\1/'
16652
16651
16607
Solution with awk and cut
vinko#parrot:~$ cat test
frontend RUNNING pid 16652, uptime 2:11:17
nginx RUNNING pid 16651, uptime 2:11:17
redis RUNNING pid 16607, uptime 2:11:32
vinko#parrot:~$ awk '{print $4}' test | cut -d, -f 1
16652
16651
16607
for nginx only:
vinko#parrot:~$ grep nginx test | awk '{print $4}' | cut -d, -f 1
16651
Take a look at pgrep, a variant of grep specially tailored for grepping process tabless.
assuming that the grep implementation supports the -o option, you could use two greps:
output \
| grep -o '^nginx[[:space:]]\+[[:upper:]]\+[[:space:]]\+pid [0-9]\+' \
| grep -o '[0-9]\+$'