I am doing a command in linux that returns an output, the command is the following:
ps -fea | grep ora_pmon | awk 'length($9) > 2 {print $9}'
and its output is as follows:
ora_pmon
ora_pmon_test
ora_pmon_PRUEBA
ora_pmon_test_1
I'm trying to get the result to be:
test
PRUEBA
test_1
The other thing is that when I delete the "ora_pmon" I get a blank line in the result. How could it be removed?
That is, you want to print the third field resulting from cutting the string based on the character
_
. To do this, simply say:That is, cut by
_
and print the third.You comment that:
Therefore, it is not that you want the third block, but from the third. So let's just tell a
cut
to print everything starting at nth with-fn-
. In this case,-f3-
.I further simplify the
grep | awk
into one thing, to leave it at:A solution that quickly occurs to me is using simple regular expressions.
Where the parameter enables PERL
-P
-like regular expressions , then we can use the metacharacter . The parameter prints only what it matches.'\K'
-o
In the regular expression
'ora_pmon_\K.*'
with the metacharacter\K
the engine pretends that it started from that position, that is, once it finds the list 'ora_pmon_' it starts from there, and with.*
selects everything after it.You can try this way if it doesn't accept the parameter
-P
That is, with the character
-
after number 3. Similar to what @fedorqui put in his answer.I forgot the good one for a moment
sed
, you could use that too.