Linux 如何使用 cut 为分隔符指定更多空格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7142735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:49:19  来源:igfitidea点击:

How to specify more spaces for the delimiter using cut?

linuxdelimitercut

提问by leslie

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?

有没有办法使用 cut 命令为更多空格指定字段分隔符?(如“”+)?例如:在下面的字符串中,我想达到值'3744',我应该说哪个字段分隔符?

$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. awkis not what I am looking for either, but how to do with 'cut'?

cut -d' '不是我想要的,因为它只适用于一个空间。 awk也不是我要找的,但是如何处理“剪切”?

thanks.

谢谢。

采纳答案by paxdiablo

Actually awkis exactlythe tool you should be looking into:

其实awk正是你应该寻找到的工具:

ps axu | grep '[j]boss' | awk '{print }'

or you can ditch the grepaltogether since awkknows about regular expressions:

或者你可以grep完全放弃因为awk知道正则表达式:

ps axu | awk '/[j]boss/ {print }'

But if, for some bizarre reason, you really can'tuse awk, there are other simpler things you can do, like collapse all whitespace to a single space first:

但是,如果出于某种奇怪的原因,您真的无法使用awk,那么您还可以做其他更简单的事情,例如先将所有空格折叠为一个空格:

ps axu | grep '[j]boss' | sed 's/\s\s*/ /g' | cut -d' ' -f5


That greptrick, by the way, is a neat way to only get the jbossprocesses and not the grep jbossone (ditto for the awkvariant as well).

grep顺便说一下,这个技巧是一种只获取jboss进程而不是进程的巧妙方法grep jbossawk变体也是如此)。

The grepprocess will have a literal grep [j]bossin its process command so will not be caught by the grepitself, which is looking for the character class [j]followed by boss.

grep进程将grep [j]boss在其进程命令中有一个文字,因此不会被grep自身捕获,它正在寻找[j]后跟boss.

This is a nifty way to avoid the | grep xyz | grep -v grepparadigm that some people use.

这是避免| grep xyz | grep -v grep某些人使用的范式的好方法。

回答by paulsm4

Personally, I tend to use awk for jobs like this. For example:

就个人而言,我倾向于将 awk 用于此类工作。例如:

ps axu| grep jboss | grep -v grep | awk '{print }'

回答by Jared Ng

One way around this is to go:

解决此问题的一种方法是:

$ps axu | grep jboss | sed 's/\s\+/ /g' | cut -d' ' -f3

to replace multiple consecutive spaces with a single one.

用一个空格替换多个连续空格。

回答by fedorqui 'SO stop harming'

awkversion is probably the best way to go, but you can also use cutif you firstly squeeze the repeats with tr:

awkversion 可能是最好的方法,但是cut如果您首先使用以下方法挤压重复,您也可以使用tr

ps axu | grep jbos[s] | tr -s ' ' | cut -d' ' -f5
#        ^^^^^^^^^^^^   ^^^^^^^^^   ^^^^^^^^^^^^^
#              |            |             |
#              |            |       get 5th field
#              |            |
#              |        squeeze spaces
#              |
#        avoid grep itself to appear in the list

回答by BMW

Another way if you must use cut command

如果您必须使用 cut 命令的另一种方法

ps axu | grep [j]boss |awk '='|cut -d' ' -f5

In Solaris, replace awk with nawkor /usr/xpg4/bin/awk

在 Solaris 中,将 awk 替换为nawk/usr/xpg4/bin/awk

回答by arielf

Shorter/simpler solution: use cuts(cut on steroids I wrote)

更短/更简单的解决方案:使用cuts(减少我写的类固醇)

ps axu | grep '[j]boss' | cuts 4

Note that cutsfield indexes are zero-based so 5th field is specified as 4

请注意,cuts字段索引从零开始,因此第 5 个字段指定为 4

http://arielf.github.io/cuts/

http://arielf.github.io/cuts/

And even shorter (not using cut at all) is:

甚至更短(根本不使用 cut )是:

pgrep jboss

回答by RobertDeRose

I like to use the tr -s command for this

我喜欢为此使用 tr -s 命令

 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.

这会将所有空白压缩到 1 个空格。这种方式告诉 cut 使用空格作为分隔符,正如预期的那样。

回答by Wayne Mehl

I am going to nominate tr -s [:blank:]as the best answer.

我将提名tr -s [:blank:]为最佳答案。

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"

为什么我们要使用cut?它有一个神奇的命令,它说“我们想要第三个字段和它之后的每个字段,省略前两个字段”

cat log | tr -s [:blank:] |cut -d' ' -f 3- 

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.

我不相信 awk 或 perl split 有等效的命令,我们不知道会有多少个字段,即通过字段 X 输出第三个字段。

回答by AAAfarmclub

I still like the way Perl handles fields with white space.
First field is $F[0].

我仍然喜欢 Perl 处理带有空格的字段的方式。
第一个字段是 $F[0]。

$ ps axu | grep dbus | perl -lane 'print $F[4]'

回答by flitz

As an alternative, there is always perl:

作为替代方案,总是有 perl:

ps aux | perl -lane 'print $F[3]'

Or, if you want to get all fields starting at field #3 (as stated in one of the answers above):

或者,如果您想从字段 #3 开始获取所有字段(如上述答案之一所述):

ps aux | perl -lane 'print @F[3 .. scalar @F]'