如何使用cut为分隔符指定更多空间? [英] How to specify more spaces for the delimiter using cut?
问题描述
是否有任何方法可以使用cut命令为更多空间指定字段定界符? (例如"+")? 例如:在以下字符串中,我希望达到值"3744",我应该说什么字段定界符?
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' '
不是我想要的,因为它仅用于一个空格.
awk
也不是我要找的东西,而是如何处理剪切"?
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'?
谢谢.
推荐答案
实际上awk
是完全您应该关注的工具:
Actually awk
is exactly the tool you should be looking into:
ps axu | grep '[j]boss' | awk '{print $5}'
或者您可以完全放弃grep
,因为awk
知道正则表达式:
or you can ditch the grep
altogether since awk
knows about regular expressions:
ps axu | awk '/[j]boss/ {print $5}'
但是,如果出于某种奇怪的原因,您真的不能使用awk
,那么您还可以执行其他一些更简单的操作,例如首先将所有空格折叠到一个空格中:
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
顺便说一下,grep
技巧是一种仅获得jboss
进程而不是grep jboss
进程(也适用于awk
变体的同上)的巧妙方法.
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).
grep
进程的进程命令中将带有文字grep [j]boss
,因此不会被grep
本身捕获,grep
本身正在寻找字符类[j]
后跟boss
.
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
.
这是避免某些人使用的| grep xyz | grep -v grep
范例的好方法.
This is a nifty way to avoid the | grep xyz | grep -v grep
paradigm that some people use.
这篇关于如何使用cut为分隔符指定更多空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!