Handling text with Perl


(0 comments)

In the previous two articles, we used Sed, AWK and Bash to solve two problems: extracting package name and extracting updates. Now we solve these two problems using Perl.


Extracting the package name
Create a source file named pkgname with the following content:


while ( <STDIN> ) {
if ( /^(.*)-.*-.*$/ ) {
print "$1\n"
}
}


Run the program:


echo omarine-update-3.2-1.x86_64.rpm | perl pkgname



Extracting updates
Create a source file named log with the following content:


$lines = 0;

while ( <STDIN> ) {

if ( /^[0-9].+\/(.+)$/ ) {
$lines++;
if ( $lines > 5 ) {
exit;
}
print $1, "\n";
}
if ( /^-+$/ ) {
exit;
}
}

Run the program:


cat ChangeLog.txt | perl log | cat -n



Currently unrated

Comments

There are currently no comments

New Comment

required

required (not published)

optional

required