1

我的文件的模式由{}多行分隔,我想找到两行具有特定 sintax 的行,在我的源文件中,有时行显示如下:

{ some text size =
          0 }

或者

{ some text size
         = 0 }

和其他情况

{some text size = 0 }

我想过滤我的源文件并加入那些匹配分隔条件的行并将其转换为一行!

4

2 回答 2

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;
    
于 2013-07-01T11:43:26.223 回答
0

又快又脏:(需要傻眼):

awk -v RS="\0" '{gsub(/ size\s*=\s*0\s*}/," 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="\0" '{gsub(/ size\s*=\s*0\s*}/," 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

}
于 2013-07-01T11:24:48.350 回答