如何从单词之前删除文本?

时间:2014-09-19 11:33:16

标签: linux bash sed

正在寻找从下面一行中删除[11:55:43] [Server thread/INFO]:的方法:

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

尝试通过使用sed尝试这样做但没有运气,因为Justin可以根据获得成就的人更改为其他用户名。

有没有办法可以在用户名称之前删除[11:55:43] [Server thread/INFO]:而无需指定它? IE浏览器。 sed from has,但保持在前面并删除任何事情?

6 个答案:

答案 0 :(得分:1)

使用sed:

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

答案 1 :(得分:1)

实现这一目的的sed命令是:

sed 's/^.*: //'

e.g。

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!]

答案 2 :(得分:0)

使用sed

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

如果匹配[(某事)](某事)[(某事)]:

,则会删除每行的开头

答案 3 :(得分:0)

这应该可以解决问题:

sed 's/.*: \([^ ]* has .*\)/\1/' file

答案 4 :(得分:0)

sed 's/^\( *\[[^]]*]\)\{2\}: //' YourFile
  • 同时允许[...]
  • 之间的任何空格
  • 假设您的样本
  • 中的2个]:内容中没有[...]
  • 印刷线与结构不匹配

posix compliance(所以--posix与GNU sed)

答案 5 :(得分:0)

A 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!]