在mac上匹配sed中的重复字符

在mac上匹配sed中的重复字符

本文介绍了在mac上匹配sed中的重复字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到 3 个或更多新行的所有实例,并仅用 2 个新行替换它们(想象一个文件有太多空白).我正在使用 sed,但如果更容易,可以使用 awk 或类似的答案来回答.

I am trying to find all instances of 3 or more new lines and replace them with only 2 new lines (imagine a file with wayyy too much white space). I am using sed, but OK with an answer using awk or the like if that's easier.

注意:我使用的是 mac,所以 sed 与 linux 上的略有不同(BSD 与 GNU)

note: I'm on a mac, so sed is slightly different than on linux (BSD vs GNU)

我的实际目标是换行,但我根本无法让它工作,所以为简单起见,我试图匹配 3 个或更多重复的 bla 并将其替换为 BLA.

My actual goal is new lines, but I can't get it to work at all so for simplicity I'm trying to match 3 or more repetitions of bla and replace that with BLA.

制作一个名为愚蠢.txt的示例文件:

Make an example file called stupid.txt:

$ cat stupid.txt

blablabla
$

我的理解是您使用正则表达式语法 thing{i,} 匹配 i 或更多事物.
我已经尝试过这种变体来匹配 3 个 bla 没有运气:

My understanding is that you match i or more things using regex syntax thing{i,}.
I have tried variations of this to match the 3 blas with no luck:

cat stupid.txt | sed 's/bla{3,}/BLA/g'      # simplest way
cat stupid.txt | sed 's/bla\{3,\}/BLA/g'    # escape curly brackets
cat stupid.txt | sed -E 's/bla{3,}/BLA/g'   # use extended regular expressions
cat stupid.txt | sed -E 's/bla\{3,\}/BLA/g' # use -E and escape brackets

现在我不知道还有什么可尝试的了!

Now I am out of ideas for what else to try!

推荐答案

如果吞下整个文件是可以接受的:

If slurping the whole file is acceptable:

perl -0777pe 's/(\n){3,}/\n\n/g' newlines.txt

你应该用任何合适的换行序列替换 \n 的地方.

Where you should replace \n with whatever newline sequence is appropriate.

-0777 告诉 perl 不要将每一行分成自己的记录,这允许跨行工作的正则表达式.

-0777 tells perl to not break each line into its own record, which allows a regex that works across lines to function.

如果您对结果满意,-i 会导致 perl 就地替换文件而不是输出到标准输出:

If you are satisfied with the result, -i causes perl to replace the file in-place rather than output to stdout:

perl -i -0777pe 's/(\n){3,}/\n\n/g' newlines.txt

您也可以这样做:-i~ 使用给定的后缀(在本例中为 ~)创建备份文件.

You can also do as so: -i~ to create a backup file with the given suffix (~ in this case).

如果吞下整个文件是不可接受的:

If slurping the whole file is not acceptable:

perl -ne 'if (/^$/) {$i++}else{$i=0}print if $i<3' newlines.txt

这将打印不是第三个(或更高)连续空行的任何行.-i 与此相同.

This prints any line that is not the third (or higher) consecutive empty line. -i works with this the same.

ps--MacOS 安装了 perl.

ps--MacOS comes with perl installed.

这篇关于在mac上匹配sed中的重复字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 07:04