Here's a Perl script that accomplishes what you've described. It includes error handling, uses the Spreadsheet::Read module to parse the Excel file, and extracts strings matching your regex pattern from column D (the 4th column) of the first sheet:
#!/usr/bin/perl
use strict;
use warnings;
use Spreadsheet::Read;
# Check if exactly one argument (the file path) is provided
if (@ARGV != 1) {
die "Usage: $0 <path_to_excel_file>\n";
}
my $excel_file = $ARGV[0];
# Verify the file exists and is readable
unless (-e $excel_file && -r $excel_file) {
die "Error: Cannot read file '$excel_file' - file does not exist or is not readable\n";
}
# Verify the file has .xlsx extension
unless ($excel_file =~ /\.xlsx$/i) {
die "Error: File '$excel_file' is not an .xlsx file\n";
}
# Read the Excel file
my $workbook;
eval {
$workbook = ReadData($excel_file, parser => 'xlsx');
};
if ($@) {
die "Error reading Excel file: $@\n";
}
# Get the first sheet
my $sheet = $workbook->[1] or die "Error: No sheets found in the Excel file\n";
# Get the maximum row number
my $max_row = $sheet->{maxrow} or die "Error: No rows found in the sheet\n";
# Regex pattern to match
my $pattern = qr/(?!flex)\w\w\w\w\.\S+-v\d+\S+/;
# Process each row, looking at column D (index 4)
for my $row (1 .. $max_row) {
# Get the cell value from column D (4th column)
my $cell = $sheet->{cell}[4][$row];
# Skip if cell is empty or undefined
next unless defined $cell && $cell ne '';
# Find all matches in the cell content
while ($cell =~ /$pattern/g) {
print "$&\n";
}
}
Explanation of the Script:
-
Shebang and Pragmas:
#!/usr/bin/perl specifies the interpreter.
use strict; use warnings; enforces good coding practices and helps catch errors.
-
Module Import:
use Spreadsheet::Read; imports the CPAN module to read Excel files. You'll need to install this module if it's not already installed (cpan Spreadsheet::Read).
-
Argument Validation:
- The script checks if exactly one argument (the file path) is provided.
- It verifies that the file exists, is readable, and has a
.xlsx extension.
-
Reading the Excel File:
ReadData($excel_file, parser => 'xlsx') reads the Excel file. The parser => 'xlsx' option ensures the correct parser is used for .xlsx files.
- The script uses
eval to catch any errors during file parsing.
-
Accessing the First Sheet:
$workbook->[1] accesses the first sheet of the workbook (index 0 contains metadata).
- The script checks if the sheet exists and has rows.
-
Regex Pattern:
- The regex
(?!flex)\w\w\w\w\.\S+-v\d+\S+ is used to match strings:
(?!flex) is a negative lookahead to exclude strings starting with "flex".
\w\w\w\w matches exactly four word characters.
\.\S+ matches a dot followed by one or more non-whitespace characters.
-v\d+\S+ matches -v followed by one or more digits and one or more non-whitespace characters.
-
Processing Column D:
- The script iterates over each row in the sheet.
$sheet->{cell}[4][$row] accesses the value in column D (index 4, since columns are 1-based in the data structure but 0-based in the array).
- If the cell is non-empty, the script applies the regex to find all matches within the cell content.
-
Output:
- Matches are printed one per line using
print "$&\n", where $& contains the matched string.
Example Usage:
Assuming you have an Excel file named data.xlsx with content in column D like:
Row 1, Column D: "abcd.xyz-v123 abc.flex-v456 efgh.uvw-v789"
Row 2, Column D: "ijkl.mno-v012"
Row 3, Column D: "flex.pqr-v345"
Running the script:
perl script.pl data.xlsx
Would output:
abcd.xyz-v123
efgh.uvw-v789
ijkl.mno-v012
Notes:
-
Dependencies:
-
Error Handling:
- The script includes robust error handling for file access, file type, and parsing issues.
-
Performance:
- For very large Excel files, the script might take some time to process, but it should handle typical use cases efficiently.
-
Regex Customization:
- If you need to modify the regex pattern, you can adjust the
$pattern variable to suit your needs.
This script should meet your requirements while being robust and user-friendly.