Why can't I sed two [or more..] empty lines to one empty line?

LanceBaynes

Why can't I sed two [or more..] empty lines to one empty line? What is the trick?

echo -e "hello\n\n\nhello2" | sed 's/^$\n^$/\n/g'
hello


hello2
Caleb

The reason your sed failed is that unless you specify a multi-line operator, it operates on the stream one line at a time. Multiple beginning of line ^ and end of line $ operators are meaningless when strung together like that if you are only looking at the text one line at a time.

The easist way to collapse multiple blank lines is with cat. From the man page:

-s, --squeeze-blank
suppress repeated empty output lines

It works like this:

$ echo -e "hello\n\n\nworld" | cat -s
hello

world

If you want to remove the blank lines entirely rather than compressing them, use grep:

$ echo -e "hello\n\n\nworld" | grep -v '^$'
hello
world

Note that if you really want to do this in sed you have to use complicated expressions and actions. Here is an example (thanks to fred) that collapses any number of sequencial blanks into a single blank line:

$ echo -e "hello\n\n\nworld" | sed -re '$!N;/^\n$/!P;D'
hello

world

You can see why cat -s is a good deal easier if collapsing multiple blank lines is all you are after!

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Change three or more empty lines into two using bash, sed or awk

分類Dev

Sed delete empty lines after a particular line number

分類Dev

Why can't I match on a Seq.empty?

分類Dev

Squeeze multiple empty lines with sed

分類Dev

How to replace one line with two lines using sed in csh on BSD?

分類Dev

Why can't I use one ssh key on more than one github repo?

分類Dev

Remove empty line before a pattern using sed

分類Dev

Why can't I put more then one dot in my JS calc

分類Dev

Why can't you have two AppRegistry.registerComponent lines?

分類Dev

Conditionally Merging two lines into one line

分類Dev

Run into error “empty beginning of file” when read.table in R with 5 or more first empty lines

分類Dev

Why sed do more that I asked?

分類Dev

How can I make split(/\t/, $STRING) detect empty values when $STRING ends with "\t\t"?

分類Dev

ScalaXML and empty lines

分類Dev

Why isn't String.Empty a constant?

分類Dev

Why cannot I define an empty function in shell?

分類Dev

Why do i get an empty bitmap?

分類Dev

How can I repeat column values if the two cells of the nextline are empty in a CSV file

分類Dev

How can I merge two files while printing a given value on resulting empty fields using AWK?

分類Dev

Stackblitz: Can't Find Empty Package

分類Dev

Can I empty the local kafka state store

分類Dev

How can I get vimperator to scroll more than one line using the j and k keys?

分類Dev

creating table using JSON objects, why last row have 'td' of two rows and one empty row at first place

分類Dev

Why I can't successfully subtract two datetime values in php?

分類Dev

How can I make an IObservable from a queue, so that the sequence doesn't end when the queue is empty?

分類Dev

Why there is an empty line begin with "*" in the result of history command on linux?

分類Dev

Bootstrap Date Range Picker with two ranges with one empty at the beginning

分類Dev

Why is the "wc" command saying that I've got only one line in a file while in fact there are really two?

分類Dev

Can I use more then one span in a link?

Related 関連記事

  1. 1

    Change three or more empty lines into two using bash, sed or awk

  2. 2

    Sed delete empty lines after a particular line number

  3. 3

    Why can't I match on a Seq.empty?

  4. 4

    Squeeze multiple empty lines with sed

  5. 5

    How to replace one line with two lines using sed in csh on BSD?

  6. 6

    Why can't I use one ssh key on more than one github repo?

  7. 7

    Remove empty line before a pattern using sed

  8. 8

    Why can't I put more then one dot in my JS calc

  9. 9

    Why can't you have two AppRegistry.registerComponent lines?

  10. 10

    Conditionally Merging two lines into one line

  11. 11

    Run into error “empty beginning of file” when read.table in R with 5 or more first empty lines

  12. 12

    Why sed do more that I asked?

  13. 13

    How can I make split(/\t/, $STRING) detect empty values when $STRING ends with "\t\t"?

  14. 14

    ScalaXML and empty lines

  15. 15

    Why isn't String.Empty a constant?

  16. 16

    Why cannot I define an empty function in shell?

  17. 17

    Why do i get an empty bitmap?

  18. 18

    How can I repeat column values if the two cells of the nextline are empty in a CSV file

  19. 19

    How can I merge two files while printing a given value on resulting empty fields using AWK?

  20. 20

    Stackblitz: Can't Find Empty Package

  21. 21

    Can I empty the local kafka state store

  22. 22

    How can I get vimperator to scroll more than one line using the j and k keys?

  23. 23

    creating table using JSON objects, why last row have 'td' of two rows and one empty row at first place

  24. 24

    Why I can't successfully subtract two datetime values in php?

  25. 25

    How can I make an IObservable from a queue, so that the sequence doesn't end when the queue is empty?

  26. 26

    Why there is an empty line begin with "*" in the result of history command on linux?

  27. 27

    Bootstrap Date Range Picker with two ranges with one empty at the beginning

  28. 28

    Why is the "wc" command saying that I've got only one line in a file while in fact there are really two?

  29. 29

    Can I use more then one span in a link?

ホットタグ

アーカイブ