bash 如何删除字符串输出的前两个单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27490379/
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-09-18 12:01:17  来源:igfitidea点击:

how to remove first two words of a strings output

linuxbashawksedcut

提问by hisere

I want to remove the first two words that come up in my output string. this string is also within another string.

我想删除输出字符串中出现的前两个单词。这个字符串也在另一个字符串中。

What I have:

我拥有的:

for servers in `ls /data/field`    
do    
  string=`cat /data/field/$servers/time`

This sends this text:

这发送此文本:

00:00 down server

I would like to remove "00:00 down" so that it only displays "server".

我想删除“00:00 down”,以便它只显示“服务器”。

I have tried using cut -d ' ' -f2- $stringwhich ends up just removing directories that the command searches.

我试过使用cut -d ' ' -f2- $stringwhich 最终只删除命令搜索的目录。

Any ideas?

有任何想法吗?

回答by Gilles Quenot

Please, do the things properly :

请把事情做好:

for servers in /data/field/*; do
    string=$(cut -d" "  -f3- /data/field/$servers/time)
    echo "$string"
done
  • backticks are deprecated in 2014 in favor of the form $( )
  • don't parse lsoutput, use globinstead like I do with data/field/*
  • 反引号在 2014 年被弃用,取而代之的是 $( )
  • 不要解析ls输出,glob而是像我一样使用data/field/*

Check http://mywiki.wooledge.org/BashFAQfor various subjects

检查http://mywiki.wooledge.org/BashFAQ了解各种主题

回答by nu11p01n73R

Use -doption to set the delimtier to space

使用-d选项将分隔符设置为空格

$ echo 00:00 down server | cut -d" "  -f3-
server

NoteUse the field number 3as the count starts from 1and not 0

注意使用字段编号3作为计数的开始,1而不是0

From man page

从手册页

  -d, --delimiter=DELIM
              use DELIM instead of TAB for field delimiter

   N-     from N'th byte, character or field, to end of line

More Tests

更多测试

$ echo 00:00 down server hello world| cut -d" "  -f3-
server hello world

The forloop is capable of iterating through the files using globbing. So I would write something like

for循环能够使用通配符遍历文件。所以我会写一些类似的东西

for servers in /data/field*
do    
  string=`cut -d" "  -f3- /data/field/$servers/time`
...
...

回答by midori

You can use sed as well:

您也可以使用 sed:

sed 's/^.* * //'

回答by Otheus

For the examples given, I prefer cut. But for the general problem expressed by the question, the answers above have minor short-comings. For instance, when you don't know how many spaces are between the words (cut), or whether they start with a space or not (cut,sed), or cannot be easily used in a pipeline (shell for-loop). Here's a perl example that is fast, efficient, and not too hard to remember:

对于给出的例子,我更喜欢cut。但是对于问题表达的一般问题,上述答案有一些小缺点。例如,当您不知道单词之间有多少空格时 (cut),或者它们是否以空格开头 (cut,sed),或者无法在管道中轻松使用 (shell for-loop)。这是一个快速、高效且不太难记住的 perl 示例:

 | perl -pe 's/^\s*(\S+\s+){2}//'

Perl's -poperates like sed's. That is, it gobbles input one line at a time, like -n, and after dong work, prints the line again. The -estarts the command-line-based script. The script is simply a one-line substitute s///expression; substitute matching regular expressions on the left hand side with the string on the right-hand side. In this case, the right-hand side is empty, so we're just cutting out the expression found on the left-hand side.

Perl 的-p操作类似于 sed。也就是说,它一次输入一行,例如-n,然后在 dong 工作后,再次打印该行。在-e启动基于命令行脚本。脚本只是一个单行替换s///表达式;将左侧的匹配正则表达式替换为右侧的字符串。在这种情况下,右侧是空的,所以我们只是删除了左侧的表达式。

The regular expression, particular to Perl (and all PLRE derivatives, like those in Python and Ruby and Javascript), uses \sto match whitespace, and \Sto match non-whitespace. So the combination of \S+\s+matches a word followed by its whitespace. We group that sub-expression together with (...)and then tell sed to match exactly 2 of those in a row with the {m,n}expression, where n is optional and m is 2. The leading \s*means trim leading whitespace.

正则表达式,特别是 Perl(以及所有 PLRE 派生类,如 Python、Ruby 和 Javascript 中的那些),用于\s匹配空格和\S匹配非空格。所以组合\S+\s+匹配一个单词,后跟它的空格。我们将该子表达式组合在一起,(...)然后告诉 sed 将一行中的 2 个与{m,n}表达式完全匹配,其中 n 是可选的,m 是 2。前导\s*意味着修剪前导空格。