【问题标题】:Replace mutiline String from file using Perl使用 Perl 替换文件中的多行字符串
【发布时间】:2016-08-02 23:34:23
【问题描述】:

在我的文件中,我有类似的行

String sql="select * from "+
             "emp_data";

我想换成这样的

String sql = Query1;

我如何使用 perl 来做到这一点

我正在使用

$curUrl ="select * from emp_data" ;
$curUrl = quotemeta $curUrl;
$newVariable = "Query1";
$data =~ s/$curUrl/$newVariable/g;

适用于单行。不适合坏人

【问题讨论】:

  • 是Java语言吗?

标签: regex perl


【解决方案1】:

试着用两个替换来做。

$curUrl ="select * from emp_data" ;
$curUrl = quotemeta $curUrl;
$newVariable = "Query1";
$data = 'String sql="select * from "+
         "emp_data";';

$data=~s/"|\+|\n\s+//g;  # Here i replace the " + and \n\s character with empty.

$data =~s/$curUrl/$newVariable/g;
print $data;    

【讨论】:

    【解决方案2】:

    您可以考虑使用这个经过测试的sed 脚本:

    sed -n '
    :1
    /^.*[+][ ]*$/ {
      h;
      :2
      n; H; /^.*[+][ ]*$/ {
        b2
      }
      x 
      s/[+\n\t\r]//g
      s/["][[:space:][:blank:]]*["]//g
      s/["][[:space:][:blank:]]*select [*] from emp_data[[:space:][:blank:]]*["]/Query1/
       p; x;
       b1;
    }'
    

    测试:

    sed -n '
    :1
    /^.*[+][ ]*$/ {
      h;
      :2
      n; H; /^.*[+][ ]*$/ {
        b2
      }
      x 
      s/[+\n\t\r]//g
      s/["][[:space:][:blank:]]*["]//g
      s/["][[:space:][:blank:]]*select [*] from emp_data[[:space:][:blank:]]*["]/Query1/
       p; x;
       b1;
    }' myfile.txt
    
    String sql=Query1;
    

    【讨论】:

      【解决方案3】:
      use strict;
      use warnings; 
      use 5.020;
      use autodie;
      use Data::Dumper;
      
      # Here, the variables $/, $^I, and @ARGV either have their default values
      # or some other values that were set by code appearing here.
      
      {
          local $/ = ";";   #local => temporarily change the value of this variable until the closing parenthesis of this block is encountered
          local $^I = ".bak";
          local @ARGV = 'data.txt';
      
          while (<>) {
              my $perl_statement = $_;
              $perl_statement =~ s/sql = .*/sql = Query1/xms ;
              print $perl_statement;  #This is redirected into the file.
          }
      }  #Automatically restores the previous values for $/, $^I, and @ARGV.
      

      $/ => 输入行分隔符(默认 => "\n")。导致&lt;$INFILE&gt; 最多读取并包括指定为一行的字符。
      $^I => 如果设置为字符串(默认 => undef),则使菱形运算符 magical,它允许您看似就地编辑文件。任何打印语句都将被写入一个新文件,其名称将与原始文件相同。如果你写$^I = ".bak",那么原始文件将存储在一个带有原始文件名加上“.bak”扩展名的文件中。空白字符串表示没有备份。
      @ARGV => 菱形运算符从该数组中的文件读取。

      示例运行:

      ~/pperl_programs$ cat data.txt
      String sql="select * from "+
                   "emp_data";
      hello word="select * from "+
                   "emp_data";
      
      ~/pperl_programs$ perl 1.pl 
      
      ~/pperl_programs$ cat data.txt
      String sql = Query1
      hello word="select * from "+
                   "emp_data";
      

      或者,也许您想替换所有出现的模式:

      use strict;
      use warnings; 
      use 5.020;
      use autodie;
      use Data::Dumper;
      
      my $pattern = q{"select * from "+
                   "emp_data"};
      
      {
          local $/ = ";"; 
          local $^I = "";
          local @ARGV = 'data.txt';
      
          while (<>) {
              my $perl_statement = $_;
              $perl_statement =~ s/= \Q$pattern/ = Query1/xms;
              print $perl_statement;
          }
      }
      

      示例运行:

      ~/pperl_programs$ cat data.txt
      String sql="select * from "+
                   "emp_data";
      hello word="select * from "+
                   "emp_data";
      
      ~/pperl_programs$ perl 1.pl 
      
      ~/pperl_programs$ cat data.txt 
      String sql = Query1;
      hello word = Query1;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-23
        • 2014-09-30
        • 1970-01-01
        • 2020-11-21
        • 2022-01-08
        • 2015-07-01
        • 1970-01-01
        相关资源
        最近更新 更多