How to Use Awk in Perl

How to Use Awk in Perl thumbnail
Awk is a Unix data extraction utility.

Awk is a Unix utility function you can use to extract data for filtering and reporting. An awk command can be called directly from the command line, a shell script or a Perl script. Although an awk script cannot be directly written inside a perl script, you can execute it using a perl system call. Two other commands, gawk and nawk, provide alternatives to awk that can be called from perl in the same way.

Instructions

    • 1

      Create your perl script with the interpreter directive locating perl on your system. This is typically "#!/usr/bin/perl" or "#!/usr/local/bin/perl," but may vary depending on your particular system parameters.

    • 2

      Execute a perl system call using the system() function:

      system (" awk ' --your awk script here--' input_file.txt ");

      The system function forks the awk process. Your perl script will wait for the awk command to finish before it continues executing.

    • 3

      Escape characters using a backslash in the awk script that perl may interpret as perl commands, such as "$" and quotation marks. Your call may look like:

      system (" awk ' {print \$5, \"\t\", \$6}' input_file.txt ");

      This awk script will print the fifth and sixth columns of "input_file.txt" separated by a tab character.

Related Searches:

References

  • Photo Credit Thinkstock/Comstock/Getty Images

Comments

Related Ads

Featured