I'm trying to remove all dialogues from a txt book, how would i achieve that? I saw posts about using sed or awk, but what would be the exact format? Thank you

  • 1
    It’s almost impossible to give an answer that will help you unless you give an example of what you have and what you want to get. For example, if the text has Chris said “I’m home.”, I guess you would want to remove that entire sentence — but what if it has Chris came in and said “I’m home.”? What about Chris said that she was home. or Pat breathlessly, happily proclaimed that he was home. or Kelly asked whether anybody was going home.?  See also How to put sentences on separate lines in Linux. – G-Man Says 'Reinstate Monica' Dec 2 '19 at 22:39
sed ':a;N;$!ba;s/"[^"]*"//g' file1 > file2

Copyright James Daniel Marrs Ritchey. This material was created for submission at 'how to remove content within double quotes from terminal in a plain text file', but can also be alternatively obtained from 'https://snippetly.blogspot.com/2019/12/delete-double-quoted-content.html' under the terms of any of the following licenses: Comprehensible Open License 3.0 (https://jamesdanielmarrsritchey.blogspot.com/2019/06/comprehensible-open-license-30.html), MIT (https://opensource.org/licenses/MIT).

To delete content between and including double quotes you could use preg_replace in PHP. Just make sure to make it non-greedy so it doesn't delete everything between the first and very last double quote.

For Text:


$text = <<<'EOT'

This is some example text. Bob, "Yah, but it's not very interesting to read." Yet

there is more example text of a similar nature. Natalie, "Please don't type anymore example

text for me to read. It's really bad." Still, more example text is typed, because it is important

that there be more text after the last dialogue example to ensure proper

function of the code. David, "I think that's plently of example text!" David was correct.


$new_text = preg_replace('/\"[\s\S]+?\"/', '', $text);

echo $new_text;


For Text File:


$text = file_get_contents($file);

$new_text = preg_replace('/\"[\s\S]+?\"/', '', $text);

file_put_contents($new_file, $new_text);


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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