[mdlug] GNU sed weirdness
John Wiersba
jrw32982 at yahoo.com
Sun Jan 24 23:40:40 EST 2010
Jeff,
I mostly agree with Aaron (quoted below). But just because you can write unreadable perl doesn't mean that you have to. I would suggest using perl to rewrite this as perl's towering strength is text manipulation. In general, perl subsumes and unifies both sed and awk functionality.
The following shell script does what I interpret you want based on your original post. Since you already have filelist.txt, you can skip the creation and removal of it and just use the perl command. This script produces the following output:
"c:\Program Files\7-Zip\7z.exe" a -mx=7 -ms=on -wD:\ "D:\archive" "D:\data\20100123.*"
-- John Wiersba
cat <<EOF >filelist.txt
D:\data\20100123.idx
D:\data\20100123.dat
EOF
perl -e '
for (@ARGV) {
($var, $val) = split /=/, $_, 2;
$opt{$var} = $val;
}
$_ = <STDIN>;
chomp;
s/\.[^.]+\z/.*/;
print qq{"$opt{compress_exe}" $opt{compress_opts} "$opt{target_dir}" "$_"\n};
exit 0;
' compress_exe='c:\Program Files\7-Zip\7z.exe' \
compress_ext='7z' \
compress_opts='a -mx=7 -ms=on -wD:\' \
target_dir='D:\archive' \
< filelist.txt
rm filelist.txt
-- John Wiersba
Date: Sun, 24 Jan 2010 03:23:14 -0500
From: Aaron Kulkis <akulkis00 at gmail.com>
Subject: Re: [mdlug] GNU sed weirdness
If in doubt, break the problem down instead of trying to do it
all in one fell swoop as a "one-liner"
One liners are one of the major reasons (the other
being the character set) for APL being a "write-only"
(i.e. unreadable.) language.
Perl suffers from the same problem -- too many perl
coders try to write programs to have the fewest number
of newlines rather than being readable (and hence,
debugable and maintainable and debugable. Did I
mention debugable?).
You're hitting the outer-edge of sed-ness.
But you are smack dab in the middle of awk-ness.
So, I would suggest redoing this in AWK instead.
(I.e, rather than trying to beating your head trying
to use a pocket knife to whittle a piece of wood into
a smooth, round, baseball bat -- something techinically
possible, but very difficult, just put your piece of
wood on a lathe, and get the job done).
More information about the mdlug
mailing list