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.
\s* - delimiter.
Delimited formats Any character may be used to separate the values, but the most common delimiters are the comma, tab, and colon. The vertical bar (also referred to as pipe) and space are also sometimes used.
The tab character is the default delimiter of cut, so by default, it considers a field to be anything delimited by a tab. Remember that the "space" between each word is actually a single tab character, so both lines of output are displaying ten characters: eight alphanumeric characters and two tab characters.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With