Linux perl Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Perl programming language in a Linux environment. You will start by introducing Perl and creating a simple script, then explore executing Perl scripts in the terminal, and finally dive into practical examples of Perl commands for file manipulation. The lab covers essential Perl programming concepts and provides hands-on experience with real-world applications of the language in a Linux setting.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") linux/VersionControlandTextEditorsGroup -.-> linux/nano("`Simple Text Editing`") subgraph Lab Skills linux/cat -.-> lab-422854{{"`Linux perl Command with Practical Examples`"}} linux/echo -.-> lab-422854{{"`Linux perl Command with Practical Examples`"}} linux/chmod -.-> lab-422854{{"`Linux perl Command with Practical Examples`"}} linux/nano -.-> lab-422854{{"`Linux perl Command with Practical Examples`"}} end

Introduction to Perl Programming in Linux

In this step, we will introduce the Perl programming language and how to use it in a Linux environment. Perl is a powerful and versatile scripting language that is widely used for system administration, text processing, and web development tasks.

First, let's check the version of Perl installed in our Ubuntu 22.04 Docker container:

perl --version

Example output:

This is perl 5, version 30, subversion 0 (v5.30.0) built for x86_64-linux-gnu-thread-multi
(with 44 registered patches, see perl -V for more detail)

Copyright 1987-2019, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

As you can see, Perl version 5.30.0 is installed in our environment.

Next, let's create a simple Perl script. Create a new file named hello.pl in the ~/project directory:

nano ~/project/hello.pl

Add the following Perl code to the file:

#!/usr/bin/env perl

print "Hello, Perl!\n";

Save the file and exit the nano editor.

Now, make the script executable and run it:

chmod +x ~/project/hello.pl
~/project/hello.pl

Example output:

Hello, Perl!

In this step, we have introduced the Perl programming language and learned how to create and execute a simple Perl script in a Linux environment. In the next steps, we will explore more advanced Perl commands and practical examples.

Executing Perl Scripts in the Linux Terminal

In this step, we will learn how to execute Perl scripts in the Linux terminal.

First, let's create another Perl script named math.pl in the ~/project directory:

nano ~/project/math.pl

Add the following Perl code to the file:

#!/usr/bin/env perl

$a = 10;
$b = 5;
$sum = $a + $b;
print "The sum of $a and $b is $sum\n";

Save the file and exit the nano editor.

Now, make the script executable and run it:

chmod +x ~/project/math.pl
~/project/math.pl

Example output:

The sum of 10 and 5 is 15

You can also run Perl scripts directly using the perl command:

perl ~/project/math.pl

This will produce the same output as before.

Additionally, you can pass arguments to your Perl script from the command line. Modify the math.pl script to accept two arguments:

#!/usr/bin/env perl

$a = $ARGV[0];
$b = $ARGV[1];
$sum = $a + $b;
print "The sum of $a and $b is $sum\n";

Save the file and run the script with arguments:

~/project/math.pl 20 10

Example output:

The sum of 20 and 10 is 30

In this step, we have learned how to execute Perl scripts in the Linux terminal, both by making the script executable and by running it directly with the perl command. We have also seen how to pass arguments to a Perl script from the command line.

Practical Examples of Perl Commands for File Manipulation

In this step, we will explore some practical examples of using Perl commands for file manipulation tasks.

First, let's create a sample text file named data.txt in the ~/project directory:

echo "Name,Age,City" > ~/project/data.txt
echo "John,30,New York" >> ~/project/data.txt
echo "Jane,25,Los Angeles" >> ~/project/data.txt
echo "Bob,35,Chicago" >> ~/project/data.txt

Now, let's use Perl to read and manipulate the contents of this file.

To read the contents of the file and print each line:

perl -e 'while (<>) { print; }' ~/project/data.txt

Example output:

Name,Age,City
John,30,New York
Jane,25,Los Angeles
Bob,35,Chicago

To extract specific fields from each line (e.g., name and city):

perl -lane 'print "$F[0], $F[2]"' ~/project/data.txt

Example output:

John, New York
Jane, Los Angeles
Bob, Chicago

To replace a specific value in the file:

perl -i -pe 's/New York/San Francisco/' ~/project/data.txt

Now, let's verify the contents of the file:

cat ~/project/data.txt

Example output:

Name,Age,City
John,30,San Francisco
Jane,25,Los Angeles
Bob,35,Chicago

In this step, we have learned how to use Perl commands to read, extract, and modify the contents of a text file. These examples demonstrate the versatility of Perl for file manipulation tasks.

Summary

In this lab, we first introduced the Perl programming language and its usage in a Linux environment. We learned how to check the installed Perl version, create a simple Perl script, make it executable, and run it. Then, we explored executing Perl scripts in the Linux terminal, including passing command-line arguments and using the shebang line to run scripts directly. Finally, we covered practical examples of Perl commands for file manipulation, such as reading, writing, and modifying files. Through these steps, we gained a solid understanding of Perl programming and its applications in a Linux system.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like