> root# ps -ef | grep [j]ava | awk '{print $2,$9}'
> 45134 -Dapex=APEC
> 45135 -Dapex=JAAA
> 45136 -Dapex=APEC
I need to put the first APEC of first as First PID, third line of APEC and Second PID and last one as Third PID.
I've tried awk but no expected result.
> First_PID =ps -ef | grep [j]ava | awk '{print $2,$9}'|awk '{if ($0 == "[^0-9]" || $1 == "APEC:") {print $0; exit;}}'
Expected result should look like this.
> First_PID=45134
> Second_PID=45136
> Third_PID=45135
CodePudding user response:
With your shown samples and attempts please try following awk
code. Written and tested in GNU awk
.
ps -ef | grep [j]ava |
awk '
{
val=$2 OFS $9
match(val,/([0-9] ) -Dapex=APEC ([0-9] ) -Dapex=JAAA\s([0-9] )/,arr)
print "First_PID="arr[1],"Second_PID=",arr[3],"Third_PID=",arr[2]
}
'
CodePudding user response:
How about this:
$ input=("1 APEC" "2 JAAA" "3 APEC")
$ printf '%s\n' "${input[@]}" | grep APEC | sed -n '2p'
3 APEC
Explanation:
input=(...)
- input data in an array, for testingprintf '%s\n' "${input[@]}"
- printinput
array, one element per linegrep APEC
- keep lines containingAPEC
onlysed -n
- runsed
without automatic printsed -n '2p'
- print only the second line