繁体   English   中英

如何删除单词前的文字?

[英]How to remove text from before a word?

寻找一种从以下行中删除[11:55:43] [Server thread/INFO]:

[11:55:43] [Server thread/INFO]: Justin has just earned the achievement [Time to Mine!]

尝试通过使用sed来做到这一点,但没有运气,因为贾斯汀的名字可以根据获得成就的人而更改为另一个用户名。

有没有一种方法可以删除用户名之前的[11:55:43] [Server thread/INFO]:而无需指定它? 就是 sed from has,但要保留在前面的单词并删除前面的所有内容?

使用sed:

sed 's/\[.*\] \[.*\]: //' file
Justin has just earned the achievement [Time to Mine!]

一个sed命令可以实现:

sed 's/^.*: //'

例如

echo '[11:55:43] [Server thread/INFO]: Justin has just earned the achievement [Time to Mine!]' |\
    sed 's/^.*: //'
Justin has just earned the achievement [Time to Mine!]

sed

sed -e 's/^\[[^]]*\][^[]*\[[^]]*\]: //'

如果它匹配[ (something) ] (something) [ (something) ]:则会删除每行的开头]:

这应该可以解决问题:

sed 's/.*: \([^ ]* has .*\)/\1/' file
sed 's/^\( *\[[^]]*]\)\{2\}: //' YourFile
  • [...]之间也要留出任何空间
  • 假设没有[ ]:在您的样本中有2个第一[...]内容
  • 印刷线不匹配,结构不变

符合posix(所以--posix与GNU sed)

一个gawk

源文件:

$ cat infile
[11:55:43] [Server thread/INFO]: Justin has just earned the achievement [Time to Mine!]
[11:58:43] [Server thread/INFO]: Martin has just earned the achievement [Time to Mine!]

码:

$ gawk -F']: '  'BEGIN{OFS=""}{$1=""}1'  infile
Justin has just earned the achievement [Time to Mine!]
Martin has just earned the achievement [Time to Mine!]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM