6 Perl One-Liners to Replace Common Linux Utilities

https://www.profitableratecpm.com/f4ffsdxe?key=39b1ebce72f3758345b2155c98e6709c

While other scripting languages ​​have grown in popularity, Perl remains a popular choice due to its robust word processing capabilities. It’s easy to create “one-liners” or very short scripts, which can even replace standalone Unix utilities. Here are some tools you can create directly from the command line.

Regex matching to replace grep

grep is one of the most useful Linux tools for examining output. It searches for text using regular expressions, a fine-grained language that lets you specify matches down to the character level. Perl is known for its regular expression matching capabilities, so much so that many other utilities and programming languages ​​advertise “Perl compatible” regular expressions.

It’s easy to set up a single-line Perl version of grep:

        
perl -ne 'print if /PATTERN/'

Let’s review this. The -n option tells the Perl interpreter to assume that this loop already exists around the program:

        
while (<>) {
   do_something
}

The -n means that Perl will take any input from standard input or a filename. The greater than and less than symbolsis Perl’s file descriptor operator. Perl will iterate through each line of the file and perform the operations you specified in the block. The pattern between the two slashes will be the regular expression you are trying to match. See theperlrun page in the Perl documentationfor more information about Perl’s command line options.

The -e will tell Perl not to search for a Perl source file, such as a file ending in .pl. This is what makes Perl one-liners possible. Putting this together we can create a small replacement for the grep program. Let’s see how it works.

Let’s say you want to search for shells running on the system. Since Linux shells usually end in sh, we can look for this pattern. To get the list of all running processes, you can use the “ps aux” command to get the list of running processes for all users and redirect the output to your one-liner with the “|” or pipe character.

We could start by simply matching the “sh” pattern:

        ps aux | perl -ne 'print if /sh/'
Perl regular expression search for "shit" in the terminal one-liner program.

This will print any line matching the pattern “sh” in the ps program output. We can also be more precise with character quantifiers. To match the “s” followed by an h:

        ps aux | perl -ne 'print if /.sh/'
    

We can also specify any character containing “sh”. This will match things like “bash” or “zsh”.

        
ps aux | perl -ne 'print if /.*sh/'

Make sure any actual Perl code in a single Perl line is enclosed in single quotes, so the shell doesn’t interpret them instead of Perl. If you don’t do this, you will receive an error message.

Since many shell names end in “sh”, we can search for anything that contains “sh” at the end of a word with the character class “\b”:

        
ps aux | perl -ne 'print if /.*sh\b/'

Not only will this work with redirected output, but it will also work on files

        
perl -ne 'print if /.*sh/' example.txt

This will print any line containing “sh” in the example.txt file.

You can also replace the inverse grep, or grep -i, using the “unless” command in Perl:

        perl -ne 'print unless /sh/'
    

Printing fields to replace awk

awk is another venerable Linux utility that is useful for working with data organized in columns, such as processes or simple databases. A Perl one-liner can also walk through this code quickly.

Suppose we wanted to display the user name and the program the user is running from the ps aux command, we would write something like this:

        
ps aux | perl -ane 'print "$F[0] $F[10]\n"'
Pass the ps aux command into a Perl one-liner to print certain fields.

The Perl code assumes the classic “while()” loop around the program, but it also divides the input into fields. Fields are stored in an array called “@F” where an array is identified by an “at” symbol (@). To get the array values, you add the name with a dollar sign and the field number as a number, starting with 0 for the first element. Perl is criticized for the alleged messiness of using “seals” like the dollar sign to identify variables, but it’s easy to understand once you know this convention.

Also pay attention to the quote. This Perl expression with the variables will need to be double quoted within the single quoted expression to ensure that the Perl interpreter sees it.

The example above will print the first column, $F[0]which is the username, while $F[10] retrieves the path name of the executable associated with this process. This is how the Linux version of ps reports its release. If you are using a BSD system you may need to adjust it. A similar one-liner would work with any other program using data arranged this way.

You can use the -F switch to specify a pattern on which to split lines. For colon-separated output, you would use something like:

        perl -F '/:/'
    

Find and replace to replace Sed

You may have performed a find and replace operation using the s/old/new/ construct either in Vim or on the command line using the sed program. Since Perl’s regular expression search and replace feature seems, uh, “inspired” by this classic Unix utility, it’s easy to translate into a single Perl line:

        perl -pe "s/old/new/"
    

This will work both with piped input and on the command line alone. I hope that if Randal Schwartz reads this he doesn’t give me the “Unnecessary Use of Cat” award. For example, to replace “dog” with “cat”

        perl -pe "s/dog/cat"
Find and replace Perl one-liner on string, "I like to pet my dog," with "dog" replaced by "cat."

You can try it on something like “I like to pet my dog.”

The word “dog” will be replaced by “cat”.

If you try this on a line like “My dog’s breath smells like dog food”, with multiple instances of the pattern you want to replace in the line, you may notice that only the first occurrence is replaced:

My cat's breath smells like dog food.

If your cat’s breath actually smells like dog food, that would be enough. You can also replace each instance by adding the original “s/old/new” operator with “g” for “global.

Let’s make sure your cat’s breath smells like cat food:

        
perl -pe "s/dog/cat/g"
Perl global search and replacement "dog" with "cat."

Now each instance of the pattern will be replaced when it appears in the row.

Sort to replace sort

You can use sorting to sort standard entries alphabetically. It’s also easy to do with just one line

        perl -e "print sort <>"
Perl one-liner sort of lists directories in the terminal.

This will tell Perl to sort the standard input, which again includes any file specified from the command line, or passed from another command, and to sort the output alphabetically, or rather according to your system’s “locale” setting. In my case it’s C.UTF-8. If your system is configured to use a different language, the rows may be sorted differently. On my system, lines with uppercase letters seem to be sorted first.

Remove duplicates to replace uniq

uniq is used in lists with sorting to remove duplicate entries. You can reproduce the functionality with this one-liner:

        
perl -ne 'print if $_ ne $prev; $prev = $_'
Printing single elements from another Perl One Liner with a Perl One Liner.

This will compare the current line to the previous one and print it if they are unequal. The variable “$_” is a shortcut for the current input line. Although this one-liner demonstrates some Perl concepts, I’ll probably stick with uniq in practice, since it only has four characters.

You can even combine one-liners with hull-level pipes. Here is an example to print single items generated by a single line that selects fields from the previously mentioned output:

        ps aux | perl -ane 'print "$F[0]\n"' | perl -ne 'print if $_ ne $prev; $prev = $_'

Replacing tr for character replacement

While you saw the previous find and replace operator work on regular expressions, the tr command in Linux works on individual characters and character classes. Say you wanted to capitalize a file, to make it look like you were constantly shouting, you would use this single line to replace tr:

        T
perl -pe 'tr/a-z/A-Z/'
Capitalize the string "Why am I suddenly screaming?" with a perl one-liner.

This will replace each lowercase character from a to z with its uppercase counterpart.


These one-liners should give you a taste of Perl’s word processing power and show why the language still has some life left in it. You can do a lot with a little code in Perl.

Related Articles

Leave a Reply

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

Back to top button