Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete text between blank lines #1

Open
flywire opened this issue Jul 12, 2022 · 6 comments
Open

Delete text between blank lines #1

flywire opened this issue Jul 12, 2022 · 6 comments

Comments

@flywire
Copy link

flywire commented Jul 12, 2022

sed can easily delete text between blank lines but gnused treats blank lines differently.

An example would be worthwhile. Two come to mind, replacing three blank lines with a single blank line and deleting a line and the following blank line (paragraph) if it contains a search string. Non-gnused:

sed 's/\n.*kill.*\n\n/\n/g' sample_text

sample_text

This is a
sample text

    kill = whatever

demonstration.

output

This is a
sample text

demonstration.
@learnbyexample
Copy link
Owner

I don't understand what you mean by GNU sed treats blank lines differently. By default, sed works only line by line. So, you cannot match across multiple lines unless you find a way to put them in the pattern space.

For the example you have given, I'd recommend using Perl:

perl -0777 -pe 's/\n.*kill.*\n\n/\n/g'

@flywire
Copy link
Author

flywire commented Jul 12, 2022

I understand the gnused N command is different from other sed implementations. Certainly, using gnused this exercise requires pattern matching.

This is close but it still leaves a double line, even with regex similar to yours above:

>sed -re '/\n/{:a;N;/\n/!ba};/kill/d' sample_text
This is a
sample text


demonstration.

@learnbyexample
Copy link
Owner

Your command is equivalent to sed '/kill/d' since \n gets stripped from each input line (gets added back when you use N, while printing, etc).

Based on this unix.stackexchange thread:

$ sed '/^$/{:a;N;/\n$/!ba; /kill/d}' ip.txt
This is a
sample text
demonstration.

/^$/ will match an empty line and /\n$/ matches the second empty line (cannot use /^$/ here since pattern space will content from previous lines). Since the entire block is deleted, use substitute if you want to insert empty line.

Instead of all this, I'd just use awk or perl for such cases instead of sed which is just too cryptic for me with all the buffers and stuff.

@flywire
Copy link
Author

flywire commented Jul 13, 2022

use substitute if you want to insert empty line.

Yes, that is what I was trying to work out. What does that command look like?

@learnbyexample
Copy link
Owner

# empty replacement string, since sed adds back the newline while printing
$ sed '/^$/{:a;N;/\n$/!ba; s/.*kill.*//}' ip.txt
This is a
sample text

demonstration.

@learnbyexample
Copy link
Owner

oops, clicked "close" issue by mistake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants