我的档案包括:
something
something else
something else again
我需要一只双管,一只双管齐下,即产生以下产出:
something
something else
something else again
换言之,我需要把多条空白线替换为一个单列空白线。 放牧/清洗是线性的。 我从未找到过一种讲卫生运动的解决办法,这种解决办法将涉及多线性格模式。
我的档案包括:
something
something else
something else again
我需要一只双管,一只双管齐下,即产生以下产出:
something
something else
something else again
换言之,我需要把多条空白线替换为一个单列空白线。 放牧/清洗是线性的。 我从未找到过一种讲卫生运动的解决办法,这种解决办法将涉及多线性格模式。
可持续发展伙伴关系衍生系统(包括全球需求评估):
仅需要cat
和-s
的备选办法,从而使其从产出中删除反复出现的空线:
cat -s
单页:<代码>-s>-squeeze-blank:压制重复的空投线。
我刚刚通过<代码>sed解决这一问题。 即使这是一个7年老的问题,有人可能认为这是有益的,因此,我用<条码>书写我的解决办法。 这里:
sed N;/^
$/D;P;D;
grep -A1 . <yourfile> | grep -v "^--$"
这一深层解决办法假定你想要:
<><>Input
line1
line2
line3
line4
line5
<><>Output
line1
line2
line3
line4
line5
实际上,如果你用单一新线取代多个新线,产出是:
something
something else
something else again
通过以下方式实现这一目标:
sed /^$/d FILE
采用<代码>awk的解决办法,用单一空白线取代几个空白线:
awk BEGIN{bl=0}/^$/{bl++;if(bl==1)print;else next}/^..*$/{bl=0;print} myfile
通常,如果我发现有点可以做我所需要的事,我会说:
awk
BEGIN {
blank = 0;
}
/^[[:blank:]]*$/ {
if (!blank) {
print;
}
blank = 1;
next;
}
{
print;
blank = 0;
} file
如果有人想要使用,
perl -00pe0 < file
与<代码>cat-s相同:
使用::
awk { /^s*$/?b++:b=0; if (b<=1) print } file
细目:
/^s*$/?b++:b=0
- ? : the ternary operator
- /^s*$/ matches a blank line
- b variable that counts consecutive blank lines (b++).
however, if the current line is non-blank, b is reset to 0.
if (b<=1) print
print if the current line is non-blank (b==0)
or if there is only one blank line (b==1).
By adjusting the regex, you can generalize it to other scenarios like squeezing multiple blank lines (">") in email: https://stackoverflow.com/a/59189823/12483961
http://stackoverflow.com/a/934451/879121”
for i in *; do FILE=$(cat -s "$i"); echo "$FILE" > "$i"; done
Use python:
s = file("filename.txt").read()
while "
" in s: s = s.replace("
", "
")
import sys
sys.stdout.write(s)
Python:
import re
import sys
sys.stdout.write(re.sub(
{2,} ,
, sys.stdin.read()))
很容易去做。 档案和类型:
:%s/
*/
/
这将将所有2个以上新线路的区块减少到2个新线。 希望这一帮助!