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.
Actually
awk
is exactly the tool you should be looking into:or you can ditch the
grep
altogether sinceawk
knows about regular expressions: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:That
grep
trick, by the way, is a neat way to only get thejboss
processes and not thegrep jboss
one (ditto for theawk
variant as well).The
grep
process will have a literalgrep [j]boss
in its process command so will not be caught by thegrep
itself, which is looking for the character class[j]
followed byboss
.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 usecut
if you firstly squeeze the repeats withtr
:My approach is to store the PID to a file in /tmp, and to find the right process using the
-S
option forssh
. That might be a misuse but works for me.Better approach might be to query for the
SSH_PID
right before killing it, since the file might be stale and it would kill a wrong process.I like to use the tr -s command for this
This squeezes all white spaces down to 1 space. This way telling cut to use a space as a delimiter is honored as expected.
Another way if you must use cut command
In Solaris, replace awk with
nawk
or/usr/xpg4/bin/awk
I am going to nominate
tr -s [:blank:]
as the best answer.Why do we want to use cut? It has the magic command that says "we want the third field and every field after it, omitting the first two fields"
I do not believe there is an equivalent command for awk or perl split where we do not know how many fields there will be, ie out put the 3rd field through field X.