การติดตามสคริปต์ 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>
HTH
กฤษณะ