Handling text with Perl

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

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.