Solution Description
In Perl, use if (/β¦/) {β¦} else {β¦} instead of /β¦/ && β¦ That way, you can print if the match is successful and the other code otherwise.
If this is not a problem, and you want to get rid of the readlink quotation of the output and closing, you can call readlink from Perl using backlinks.
Result code
I turned everything into a single Perl program, used File::Find instead of the find , suggested $fl at the end of print in Perl is a relic (ignored it), and used Cwd::realpath() to search for the canonical file path instead of GNU readlink -f coreutils. If you still want to use readlink -f , feel free to change Cwd::realpath($_) to `readlink -f '$_'` (including backlinks!), But then it will not work for file names containing one quotation mark.
You should call this script as ./script-name starting-directory > bss.csv . If you put it in the directory you are studying, the output will also contain it along with bss.csv .
#!/usr/bin/perl # Usage: ./$0 [<starting-directory>...] use strict; use warnings; use File::Find; use Cwd; no warnings 'File::Find'; sub handleFile() { return if not -f; if ($File::Find::name =~ /\/(\d{2})\/(\d{2})\/(\d+).*-([a-zA-Z]+)(?:_(\d{1}))?/) { local $, = ';', $\ = "\n"; print map "\"$_\"", 0, $1.$2.$3, $4, $5, Cwd::realpath($_); } else { print STDERR "File $File::Find::name did not match\n"; } } find(\&handleFile, @ARGV ? @ARGV : '.');
For reference, I also enclose a polished version of the original program. It calls readlink from Perl, as I suggested above, and really uses the -n option to Perl, avoiding the while read .
#!/bin/sh find . -type f | perl -n -e 'm{/(\d{2})/(\d{2})/(\d+).*-([a-zA-Z]+)(?:_(\d{1}))?} && print qq{"0";"$1$2$3";"$4";"$5";"`readlink -f -n '\''$_'\''`"}' > bss.csv
Other source code notes
echo | before readlink does nothing and should be deleted. Readlink does not read its stdin.- Where does
$fl come from at the end of print in Perl? I guess this is a relic. - Using common quotation marks, such as
qq{} and deliberate use of delimiters (for example, when matching regular expressions and other operators like quotation marks), can save you from quoting hell. I have already used this tip above: /β¦/ β m{β¦} and "β¦" β qq{β¦} . Thanks Slade ! See the perlop manpage for more information.
Palec
source share