Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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] other characters in logs from sendmail.... :)

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

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

share|improve this question

3 Answers 3

up vote 14 down vote accepted
 echo "] 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
share|improve this answer

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.

locked by Michael Hampton Mar 28 at 22:43

This post has been locked while disputes about its content are being resolved. For more info visit meta.

How about cut instead:

cat logfile | cut -d "]" -f1
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.