bash - 连接图案之间的线条

  显示原文与译文双语对照的内容
0 0

我有一些文件,其中包含由 {} 分隔的模式,并且我希望在源文件上找到两个具有特定sintax的行,源文件上有时会出现这样的行:


{ some text size =
 0 }

或者


{ some text size
 = 0 }

还有其他情况

{some text size = 0 }

我想过滤我的源文件并连接那些符合分离条件的行,并将它的转换为一行 !

时间: 原作者:

0 0

快速和脏:( gawk需要):


awk -v RS="" '{gsub(/sizes*=s*0s*}/," size = 0}")}1' file

用你的问题文本作为内容的文件进行测试:


kent$ cat test.txt
I have files with paterns delimited by {}, on my source files sometimes I have two lines like this:
{ some text size =
 0 }
or
{ some text size
 = 0 }
and other cases
{some text size = 0 }
{this should stay as it is

}

kent$ awk -v RS="" '{gsub(/sizes*=s*0s*}/," size = 0}")}1' test.txt
I have files with paterns delimited by {}, on my source files sometimes I have two lines like this:
{ some text size = 0}
or
{ some text size = 0}
and other cases
{some text size = 0}
{this should stay as it is

}

原作者:
0 0

Perl解决方案:

  • 简单命令行:

    
    perl -pe 'local $/; s/({.*size *(?:= *)?)n/$1/g;' infile> outfile
    
    
  • 脚本:

    
    #!/usr/bin/env perl
    
    use strict;
    use warnings;
    
    $filename ="/path/to/file"
    
    open FILE,"<$filename" or die $!;
    local $/;
    my $txt = <FILE>;
    close FILE;
    
    $txt =~ s/({.*size) *(?:= *n|n *=) *(0 *})/$1 = $2/g;
    
    open FILE,">$filename" or die $!;
    print FILE $txt;
    close FILE;
    
    
原作者:
...