16

How to delete all characters in one line after "]" with sed ?

Im trying to grep some file using cat, awk. Now my oneliner returns me something like

121.122.121.111] other characters in logs from sendmail.... :)

Now I want to delete all after "]" character (with "]"). I want only 121.122.121.111 in my output.

I was googling for that particular example of sed but didn't find any help in those examples.

B14D3
  • 5,110
  • 13
  • 58
  • 82

3 Answers3

27
 echo "121.122.121.111] other characters in logs from sendmail...." | sed 's/].*//' 

So if you have a file full of lines like that you can do

 sed 's/].*//' filename
Mike
  • 21,910
  • 7
  • 55
  • 79
14

How about cut instead:

cat logfile | cut -d "]" -f1
Sven
  • 97,248
  • 13
  • 177
  • 225
3

Something like

sed 's|\(.*\)\] .*$|\1|'

should do what you want. The \(.*\)] will capture all the text up to the ] into a remembered pattern and then the \1 substitutes it for the whole line.

user9517
  • 114,104
  • 20
  • 206
  • 289