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
how to remove first two words of a strings output
提问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- $string
which ends up just removing directories that the command searches.
我试过使用cut -d ' ' -f2- $string
which 最终只删除命令搜索的目录。
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
ls
output, useglob
instead like I do withdata/field/*
- 反引号在 2014 年被弃用,取而代之的是
$( )
- 不要解析
ls
输出,glob
而是像我一样使用data/field/*
Check http://mywiki.wooledge.org/BashFAQfor various subjects
回答by nu11p01n73R
Use -d
option to set the delimtier to space
使用-d
选项将分隔符设置为空格
$ echo 00:00 down server | cut -d" " -f3-
server
NoteUse the field number 3
as the count starts from 1
and 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 for
loop 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 -p
operates like sed's. That is, it gobbles input one line at a time, like -n
, and after dong work, prints the line again. The -e
starts 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 \s
to match whitespace, and \S
to 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*
意味着修剪前导空格。