我必须处理一些非常老的代码,这些代码经常重复出现。因此,在尝试清除这些代码时,我遇到了这个问题,因为它的规模非常大。
<A>
hello! my inside contents can vary
5
</A>
我认为没有任何合理的方法可以做到这一点,但我想替换整个 A 并留下
blah(x)
其中 x 是在 A 中找到的第一个数字。
答案1
遵循 perl 脚本就可以了。
#! /usr/bin/env perl
# ------------------------------------------------
# Author: krishna
# Created: Sat Sep 22 09:50:06 2018 IST
# USAGE:
# process.pl
# Description:
#
#
# ------------------------------------------------
$num = undef;
# Process the first argument as file and read the lines into $_
while (<>) {
# remove newline at the end
chomp;
# True for all lines between the tag A
if (/<A>/ ... /<\/A>/) {
# Only when num is not defined, Capture only first occurance of a number
$num = $& if not defined $num and /\d+/;
} else {
# Print other lines as it is
printf "$_\n";
}
# After processing the tag, print the number and set to undef to capture next occurance
if (/<\/A>/) {
printf "blah($num)\n";
$num = undef;
}
}
跑步
0 > perl ./process.pl file
blah(5)
blaaaaaaaaaa
blah(50)
内容file
0 > cat file
<A>
hello! my inside contents can vary
5
505
</A>
blaaaaaaaaaa
<A>
hello! my inside contents can vary
50
</A>
高血压
克里希纳