Muser
Muser

Reputation: 11

How to remove text from before a word?

Looking for a way to remove [11:55:43] [Server thread/INFO]: from the line below:

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

Have attempted to do this through using sed but had no luck, as the name Justin can change to another users name depending on who gains an achievement.

Is there a way I can remove [11:55:43] [Server thread/INFO]: before the users name without having to specify it? Ie. sed from has, but keep the word in front and remove anything ahead of that?

Upvotes: 0

Views: 142

Answers (6)

Juan Diego Godoy Robles
Juan Diego Godoy Robles

Reputation: 14945

A gawk.

Source file:

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

Code:

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

Upvotes: 0

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed 's/^\( *\[[^]]*]\)\{2\}: //' YourFile
  • Allow also any space between [...]
  • assuming there is no ]: in 2 first [...] content like in your sample
  • printing lines no matching the structure unchanged

posix compliant (so --posix with GNU sed)

Upvotes: 0

Jens
Jens

Reputation: 72629

This should do the trick:

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

Upvotes: 0

Juan Cespedes
Juan Cespedes

Reputation: 1363

With sed:

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

That will remove the start of each line if it matches [(something)](something)[(something)]:

Upvotes: 0

isedev
isedev

Reputation: 19601

A sed command to achieve that is:

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

Upvotes: 1

anubhava
anubhava

Reputation: 784918

Using sed:

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

Upvotes: 1

Related Questions