tags:

views:

59

answers:

3

I have

text text text [text text] \n
text text text [text text] \n

I want

text text text \n
text text text \n

What RE can I use?

+2  A: 

/[^\[]*/ gets you most of the way. If nothing ever appears after [...], then it is sufficient.

wallyk
The OP may want to add a space on one end or th'other of that regex, if spaces are interesting.
dash-tom-bang
Can you explain what else you're going to do with that RE? It matches a pattern of 'anything but backslash or open square bracket', so you have some work to do to use it in a substitute operation, don't you?
Jonathan Leffler
+4  A: 

The following works regardless of whether the \n is the end of line or a backslash and a letter 'n':

sed 's/\[.*] //' $file

The pattern looks for an open square bracket followed by any sequence of characters up to the last close square bracket and a following space (if there are several). You could refine the '.*' into '[^]]*' to match anything that is not a close square bracket; this only deletes to the first close square bracket.

Jonathan Leffler
+1  A: 

you can use awk

$ more file
text [dont want text here ] text text [ dont want text] \n
text text text [ dont want text] \n

$ awk -vRS=']'  '/\[/{gsub(/\[.*/,"")}1' ORS="" file
text  text text  \n
text text text  \n
ghostdog74