AWK & Bash

We continue to explore another interesting example of text processing. That is, extract the package name from the full .rpm package file name, in two ways: using AWK and using Bash.
We take the package file omarine-update-3.2-1.x86_64.rpm to illustrate, and will extract to get “omarine-update”

Using AWK
You compose a source file named pkgname.awk with the following content:

{    
    OLDFS = FS
    FS = "-"
    $0 = $0
    printf "%s", $1
    for (i = 2; i <= NF - 2; i++)
        printf "%s", "-"$i
    print ""
    FS = OLDFS
}

Run the program:

echo omarine-update-3.2-1.x86_64.rpm | awk -f pkgname.awk

Using Bash
We can use Bash itself to output “omarine-update”. You run these commands:

fullname=omarine-update-3.2-1.x86_64.rpm
pkgname=${fullname%-*}
echo ${pkgname%-*}

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.