How to specify more spaces for the delimiter using cut?
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.