Cli命令删除特定的换行符

时间:2017-10-09 10:03:41

标签: perl awk sed command-line-interface

给定一个包含大量这些块的markdown文件:

```
json :
{
  "something": "here"
}
```

我想把所有这些都变成有效的降价,即:

```json
{
  "something": "here"
}
```

如何有效地跨越任意数量的文件?

我已经谷歌搜索了一下,发现了类似的问题,但无法将他们的解决方案转换为我的特定需求。似乎SED在多行匹配方面并不出色,并且包含`字符显然也会导致问题。

我试过

perl -pe "s/\njson :/json/g"

但是没有给出任何匹配。

1 个答案:

答案 0 :(得分:4)

要使Perl程序正常工作,您需要更改input record separator $/。在程序运行BEGIN循环之前,一个简单的undef块将对while进行处理。

foo 是您的输入文件。

$ perl -pe 'BEGIN{undef $/} s/\njson :/json/g' foo
```json
{
  "something": "here"
}

Perl现在会立刻在整个文件中啜饮,对于降价文档应该没问题。如果你想处理几GB大小的文件,可以获得更多的RAM。

请注意,您还需要-i进行就地编辑。

$ perl -pi -e '...' *

更短的版本是使用-0标志而不是BEGIN块来告诉Perl输入记录分隔符。 perlrun说:

  

特殊值00将导致Perl在段落中粘贴文件            模式。任何值0400或更高将导致Perl整个文件,            但按照惯例,值0777是通常用于此的值            目的

您可以通过使用re 'debug' pragma运行程序来自己检测到这一点,这会打开正则表达式的调试模式。它会告诉你的。

$ perl -Mre=debug -pe 's/\njson :/json/g' foo
Compiling REx "\njson :"
Final program:
   1: EXACT <\njson :> (4)
   4: END (0)
anchored "%njson :" at 0 (checking anchored isall) minlen 7 
Matching REx "\njson :" against "```%n"
Regex match can't succeed, so not even tried
```
Matching REx "\njson :" against "json :%n"
Intuit: trying to determine minimum start position...
  Did not find anchored substr "%njson :"...
Match rejected by optimizer
json :
Matching REx "\njson :" against "{%n"
Regex match can't succeed, so not even tried
{
Matching REx "\njson :" against "  %"something%": %"here%"%n"
Intuit: trying to determine minimum start position...
  Did not find anchored substr "%njson :"...
Match rejected by optimizer
  "something": "here"
Matching REx "\njson :" against "}%n"
Regex match can't succeed, so not even tried
}
Matching REx "\njson :" against "```%n"
Regex match can't succeed, so not even tried
```
Matching REx "\njson :" against "%n"
Regex match can't succeed, so not even tried

Freeing REx: "\njson :"

赠品是这样的:

Matching REx "\njson :" against "```%n"
Regex match can't succeed, so not even tried