- Perl ›
- File input/output ›
- here
Write a filter program that can specify options
Let's write a filter program that can specify options with command line arguments. You can specify the position of the column to be extract and the delimiter as options.
Use while statement and the line input operator to read the line.
use strict; use warnings; # Module for handling command line options use Getopt::Long 'GetOptions'; # Write Unix-like filter programming. # This time, it is a filter program that extracts the first column of comma-separated strings. # Default value if no command line option is specified my $col = 1; my $sep = ','; # Receive command line options GetOptions( 'col = i'=> \$col, 'sep = s'=> \$sep, ); # A character with a special meaning was specified in a regular expression such as . # Escape the string to work correctly even if $sep = quotemeta($sep); # Special line input operator while (<>) { # When the argument is omitted, the chomp function targets $_. chomp; # The third argument of split, -1, is when the last column is an empty string. # Also specified to work correctly # The split function returns a list, so only the parts you need # Get by array slice my $item = (split(/ $sep /, $_, -1)) [$col -1]; print $item . "\n"; }
(Reference) Regular expression
The following is a file for reading.
masao: 10:Japan taro: 20:USA rika: 38:France
Code explanation
(1) How to specify command line options
use Getopt::Long;
Getopt::Long makes it easy to specify command line options. To specify command line options:
If you're not sure about command line arguments, see the following articles first.
# Specification method 1 (- name val format) perl script name - col 1 - sep: filename # Specification method 2 (- name = val format) perl script name - col = 1 - sep =: file name # Specification method 3 (abbreviated form) perl script name -c1 -s: file name
(2) GetOptions function
The GetOption function is a function that makes it easy to receive command line options. It will be imported when you use Getopt::Long.
Below is an example of how to write.
# Default value if no command line option is specified my $col = 1; my $sep = ','; # Receive command line options GetOptions( 'col = i'=> \$col, 'sep = s'=> \$sep, );
First, set the default values for command line options.
Then call the GetOption function for the number of options you want to specify
'Option name = Value type' => Reference to variable
I will arrange the description. The value type can be s, which represents a string, i, which represents a number, and so on.
(3) Improvement of line reading in while loop
while (<>) { chomp; my $item = (split(/ $sep /, $_, -1)) [$col -1]; print $item, "\n"; }
For more information on while loop processing, see the following article.
(3) - 1 Omission of $_
chomp;
chomp function If you omit the argument, the target that works is predefined variable "$_ Will be. There are many other functions in Perl that act on $_ if you omit the argument.
(3) - Specify - 1 as the third argument of the - 2 split function
split(/ $sep /, $_, -1)
If the third argument of split function is omitted and the end of the element is blank, the last empty string will be ignored. (It was pointed out by Mr. huta.)
taro, 10,
If you read, what you want is
('taro', 10, '')
Even though it is a list
('taro', 10)
Will be got. To solve this, you can make the last empty string recognized by giving -1 to the third argument of split.
(3) - 3 Get only the elements specified in the array slice
my $item = (split(/ $sep /, $_, -1)) [$col -1];
Since split returns a list, array slice gets only the specified elements.