使用正则表达式

时间:2015-09-02 17:21:06

标签: regex awk

我有一个内容如下的文件,没有新行:

1234%#@@!#12346@!#4562366@!#@!#@!#+++++456789%#@@!#12346@!#4562366@!#@!#@!#

我希望将文件修改为

1234%#@@!#@!#@!#@!#@!#+++++456789%#@@!@!#@!#@!#@!#

即。在分隔符@!#和下一个分隔符@!#之间的任何内容都应该为空。

任何指向此的指针都会很有用。非常感谢

1 个答案:

答案 0 :(得分:4)

这里有一些冗长的perl:

use strict;

# read the file, the filename is given as a parameter to the perl script
open my $f, "<", shift; 
my $data = <$f>; 
close $f;

# the possible delimeters
my @delims = qw( @!# %#@ +++++ );

# split the data into an array of intermixed fields and delimiters
my $delim_re = join "|", map {quotemeta} @delims;
my @fields_and_delims = split /($delim_re)/, $data;;

for (my $i=2; $i < @fields_and_delims; $i+=2) {
    # empty this element if between "@!#" delimiters
    $fields_and_delims[$i] = "" if $fields_and_delims[$i-1] eq $delims[0] 
                               and $fields_and_delims[$i+1] eq $delims[0];
}

# the output, with no trailing newline
print join "", @fields_and_delims;

然后

$ printf "%s" "1234%#@@!#12346@!#4562366@!#@!#@!#+++++456789%#@@!#12346@!#4562366@!#@!#@!#+++++‌​++201546987@!#123456@!#4562366@!#@!#@!#+++++++" > file
$ perl script.pl file
1234%#@@!#@!#@!#@!#@!#+++++456789%#@@!#@!#@!#@!#@!#+++++‌​++201546987@!#@!#@!#@!#@!#+++++++