5

I'm having trouble figuring how to delete a string in between parentheses only when it is in parentheses. For example, I want to delete the string "(Laughter)", but only when it's in parenthesis and not just make it a case sensitive deletion since a sentence within that string starts with Laughter.

Teddy T
  • 51
  • 1
  • 1
  • 3
  • 2
    Have you tried anything? – anubhava Jan 07 '15 at 18:22
  • I've tried everything I can think of. I just tried MightyPork's answer and that didn't work. The only thing that I haven't tried that I can think of is searching for all strings in parens with grep then deleting them with sed or something. I don't know I'm at a complete loss. – Teddy T Jan 07 '15 at 19:27
  • your question is very unclear, but my commands work. – MightyPork Jan 07 '15 at 19:28
  • Specifically: there is a sentence that reads "word word (Laughter) words word words. Laughter words word." how do I delete the "(Laughter)" and not the other "Laughter" not in parentheses that begins the second sentence? Because I just get "word word Laughter words word words. Laughter words word." from it. – Teddy T Jan 07 '15 at 19:32
  • 1
    That works perfectly fine: http://jpeg.cz/images/2015/01/08/480J3.png – MightyPork Jan 08 '15 at 16:47
  • Indeed @MightyPork answer does work. You must be leaving something out in your question, or you're not implementing it correctly. – buydadip Jan 08 '15 at 17:03
  • @TeddyT it might also help to provide an excerpt of the file you wish to change. – buydadip Jan 08 '15 at 17:10

2 Answers2

8

I'm not sure I understand you correctly, but the following will remove text between parentheses:

sed "s/[(][^)]*[)]/()/g" myfile

(or as Llama pointed out in comments, just:)

sed "s/([^)]*)/()/g" myfile

It matches a literal open paren [(] followed by any number of non-) characters [^)]* followed by a literal close paren [)].

Example:

$ echo "Blah blah (potato) moo (cow is a pretty bird)(hello)" | sed "s/[(][^)]*[)]/()/g"
Blah blah () moo ()()

Use // instead of /()/ there if you don't want the empty parens in the output.

MightyPork
  • 18,270
  • 10
  • 79
  • 133
  • In sed, you don't *need* the parens in character classes (although it's probably not a bad habit to play it safe). The following works fine: `sed "s/([^)]*)/()/g"` – Mr. Llama Jan 07 '15 at 18:28
  • That's because by default (GNU) sed uses "basic" regular expressions, where grouping is done with `\(` and `\)`, not `(` and `)`. see https://www.gnu.org/software/gnulib/manual/html_node/Regular-expression-syntaxes.html – glenn jackman Jan 07 '15 at 18:36
  • 1
    Don't use `cat file | sed`, use `sed '...' file` – Tom Fenech Jan 07 '15 at 18:37
  • Works too, yeah. Not much of a difference of you just wanna check if it works, no? – MightyPork Jan 07 '15 at 18:41
  • 1
    I think @TomFenech is referring to [`The Useless Use of Cat`](http://www.smallo.ruhr.de/award.html#cat). There are some edge cases where using `cat` may lead to unexpected results. Here's a [SO question](http://stackoverflow.com/q/11710552/477563) on the topic. – Mr. Llama Jan 07 '15 at 18:46
3

Here's an awk solution because, why not?

awk '{gsub("[(][^)]*[)]","")}1' file

OR

awk -F '[(][^)]*[)]' '{for(i=1; i<=NF; i+=1)printf("%s",$i);printf("\n")}' file

Remember that in order to escape parentheses use must enclose them in [] brackets.

By using [^)]*, as @MightyPork did in his answer, which indicates any number of characters that are not parentheses, will reduce the greed of the match. In contrast, using .* instead, would result in a greedy match.

buydadip
  • 8,890
  • 22
  • 79
  • 154