I tried:
我试着:
sed -i 's/\n+/\n/' file
but it's not working.
但这不是工作。
I still want single line breaks.
我仍然想要单线插播。
Input:
输入:
abc
def
ghi
jkl
Desired output:
期望的输出:
abc
def
ghi
jkl
6 个解决方案
#1
3
Sed isn't very good at tasks that examine multiple lines programmatically. Here is the closest I could get:
Sed并不擅长以编程方式检查多行。这是我能得到的最接近的结果:
$ sed '/^$/{n;/^$/d}' file
abc
def
ghi
jkl
The logic of this: if you find a blank line, look at the next line. If that next line is also blank, delete that next line.
这样做的逻辑是:如果你找到一条空行,看看下一行。如果下一行也是空的,那么删除下一行。
This doesn't gobble up all of the lines in the end because it assumes that there was an intentional extra pair and reduced the two \n\n
s down to two \n
s.
这并没有把所有的线都吃光,因为它假设有一个有意的额外的一对,并把两个\n减少到两个\n。
To do it in basic awk
:
在基本awk中完成:
$ awk 'NF > 0 {blank=0} NF == 0 {blank++} blank < 2' file
abc
def
ghi
jkl
This uses a variable called blank, which is zero when the number of fields (NF
) is nonzero and increments when they are zero (a blank line). Awk's default action, printing, is performed when the number of consecutive blank lines is less than two.
它使用一个名为blank的变量,当字段数(NF)为非零时为零,当字段数为零时为增量(空行)。Awk的默认操作是打印,当连续空行数小于2时执行。
#2
1
perl -00 -pe 1 filename
That splits the input file into "paragraphs" separated by 2 or more newlines, and then prints the paragraphs separated by a single blank line:
将输入文件分成两行或两行以上的“段落”,然后打印用一行空行分隔的段落:
perl -00 -pe 1 <<END
abc
def
ghi
jkl
END
abc
def
ghi
jkl
#3
1
Using awk (gnu or BSD) you can do:
使用awk (gnu或BSD),您可以:
awk -v RS= -v ORS='\n\n' '1' file
abc
def
ghi
jkl
Also using perl
:
也使用perl:
perl -pe '$/=""; s/(\n)+/$1$1/' file
abc
def
ghi
jkl
#4
0
This might work for you (GNU sed):
这可能对你有用(GNU sed):
sed '/^$/{:a;N;s/\n$//;ta}' file
This replaces multiple blank lines by a single blank line.
这将用一个空行替换多个空行。
However if you want to place a blank line after each non-blank line then:
但是,如果您想在每个非空行之后放置一个空行,则:
sed '/^$/d;G' file
Which deletes all blank lines and only appends a single blank line to a non-blank line.
它删除所有的空行,只将一个空行附加到一个非空行。
#5
-1
Why not just get rid of all your blank lines, then add a single blank line after each line? For an input file tmp
as you specified,
为什么不去掉所有的空行,然后在每一行后面添加一个空行呢?对于您指定的输入文件tmp,
sed '/^$/d' tmp|sed '0~1 a\ '
abc
def
ghi
jkl
If white space (spaces and tabs) counts as a "blank" line for you, then use sed '/^\s*$/d' tmp|sed '0~1 a\ '
instead.
如果空白(空格和制表符)计数作为一个“空白”线,然后使用sed ' / ^ \ s *美元/ d ' tmp | sed“0 ~ 1 \”。
Note that these solutions do leave a trailing blank line at the end, as I wasn't sure if this was desired. Easily removed.
请注意,这些解决方案在最后会留下一个尾随的空行,因为我不确定是否需要这样做。轻松地删除。
#6
-1
This gives you what you want using solely sed :
这为您提供了使用单独sed所需的内容:
sed '/^$/d' txt | sed -e $'s/$/\\\n/'
The first sed command removes all empty lines, denoted as "^$".
第一个sed命令删除所有空行,指示为“^ $”。
The second sed command inserts one newline character at the end of each line.
第二个sed命令在每一行的末尾插入一个换行字符。
#1
3
Sed isn't very good at tasks that examine multiple lines programmatically. Here is the closest I could get:
Sed并不擅长以编程方式检查多行。这是我能得到的最接近的结果:
$ sed '/^$/{n;/^$/d}' file
abc
def
ghi
jkl
The logic of this: if you find a blank line, look at the next line. If that next line is also blank, delete that next line.
这样做的逻辑是:如果你找到一条空行,看看下一行。如果下一行也是空的,那么删除下一行。
This doesn't gobble up all of the lines in the end because it assumes that there was an intentional extra pair and reduced the two \n\n
s down to two \n
s.
这并没有把所有的线都吃光,因为它假设有一个有意的额外的一对,并把两个\n减少到两个\n。
To do it in basic awk
:
在基本awk中完成:
$ awk 'NF > 0 {blank=0} NF == 0 {blank++} blank < 2' file
abc
def
ghi
jkl
This uses a variable called blank, which is zero when the number of fields (NF
) is nonzero and increments when they are zero (a blank line). Awk's default action, printing, is performed when the number of consecutive blank lines is less than two.
它使用一个名为blank的变量,当字段数(NF)为非零时为零,当字段数为零时为增量(空行)。Awk的默认操作是打印,当连续空行数小于2时执行。
#2
1
perl -00 -pe 1 filename
That splits the input file into "paragraphs" separated by 2 or more newlines, and then prints the paragraphs separated by a single blank line:
将输入文件分成两行或两行以上的“段落”,然后打印用一行空行分隔的段落:
perl -00 -pe 1 <<END
abc
def
ghi
jkl
END
abc
def
ghi
jkl
#3
1
Using awk (gnu or BSD) you can do:
使用awk (gnu或BSD),您可以:
awk -v RS= -v ORS='\n\n' '1' file
abc
def
ghi
jkl
Also using perl
:
也使用perl:
perl -pe '$/=""; s/(\n)+/$1$1/' file
abc
def
ghi
jkl
#4
0
This might work for you (GNU sed):
这可能对你有用(GNU sed):
sed '/^$/{:a;N;s/\n$//;ta}' file
This replaces multiple blank lines by a single blank line.
这将用一个空行替换多个空行。
However if you want to place a blank line after each non-blank line then:
但是,如果您想在每个非空行之后放置一个空行,则:
sed '/^$/d;G' file
Which deletes all blank lines and only appends a single blank line to a non-blank line.
它删除所有的空行,只将一个空行附加到一个非空行。
#5
-1
Why not just get rid of all your blank lines, then add a single blank line after each line? For an input file tmp
as you specified,
为什么不去掉所有的空行,然后在每一行后面添加一个空行呢?对于您指定的输入文件tmp,
sed '/^$/d' tmp|sed '0~1 a\ '
abc
def
ghi
jkl
If white space (spaces and tabs) counts as a "blank" line for you, then use sed '/^\s*$/d' tmp|sed '0~1 a\ '
instead.
如果空白(空格和制表符)计数作为一个“空白”线,然后使用sed ' / ^ \ s *美元/ d ' tmp | sed“0 ~ 1 \”。
Note that these solutions do leave a trailing blank line at the end, as I wasn't sure if this was desired. Easily removed.
请注意,这些解决方案在最后会留下一个尾随的空行,因为我不确定是否需要这样做。轻松地删除。
#6
-1
This gives you what you want using solely sed :
这为您提供了使用单独sed所需的内容:
sed '/^$/d' txt | sed -e $'s/$/\\\n/'
The first sed command removes all empty lines, denoted as "^$".
第一个sed命令删除所有空行,指示为“^ $”。
The second sed command inserts one newline character at the end of each line.
第二个sed命令在每一行的末尾插入一个换行字符。