[英]awk command to replace string using regex
I have a file with content as below, there are no new lines:我有一个内容如下的文件,没有新行:
1234%#@@!#12346@!#4562366@!#@!#@!#+++++456789%#@@!#12346@!#4562366@!#@!#@!#
i want the file to be modified as我希望文件被修改为
1234%#@@!#@!#@!#@!#@!#+++++456789%#@@!@!#@!#@!#@!#
ie anything coming between delimiter @!#
and the next delimiter @!#
should be made null.即分隔符
@!#
和下一个分隔符@!#
之间的任何内容都应该为空。
Any pointers to this will be useful.任何指向此的指针都会很有用。 Thanks a lot
非常感谢
Here's some verbose perl:这是一些冗长的 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;
Then然后
$ printf "%s" "1234%#@@!#12346@!#4562366@!#@!#@!#+++++456789%#@@!#12346@!#4562366@!#@!#@!#+++++++201546987@!#123456@!#4562366@!#@!#@!#+++++++" > file
$ perl script.pl file
1234%#@@!#@!#@!#@!#@!#+++++456789%#@@!#@!#@!#@!#@!#+++++++201546987@!#@!#@!#@!#@!#+++++++
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.