简体   繁体   中英

Piping grep matches into sed

I have a file that might look like this:

EntryName1
1234
1234
1234
EntryName2

EntryName3
1234
1234
1234
[...]

I need to remove all entries that are followed by an empty line. I can grep the empty line and the repective entryName with grep -E '^$' -B 1 myFile.txt .

Now I would like to somehow pipe these as patterns into sed, in order to remove those lines from the file. How do I do that?

Edit: The expected output would look like this (leaving the blank line would also be okay).

EntryName1
1234
1234
1234
EntryName3
1234
1234
1234
[...]

A shorter awk :

awk 'p != "" && NF{print p} {p=$0} END{print p}' file

EntryName1
1234
1234
1234
EntryName3
1234
1234
1234
[...]

Use awk.

awk '/^$/ { a = ""; next }
     { printf "%s", a; a = $0 ORS }
     END { printf "%s",a }' file

带有gawk和multichar RS支持:

gawk 'BEGIN{RS="\n[^\n]+\n\n"}1' file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM