by dnet · 7y ago · 0 comments
$ echo -e '0001\n0010\n0002' | sed 's/^0*//'
1
10
2So instead of scripting, he could have generated a sorted list of numbers from the files he had. Created a file with the sequence of numbers for the range and diffed/commed the whole thing. Voilà...
$ seq -w 0001 0003
0001
0002
0003
$ $ seq -w 98 102
098
099
100
101
102 seq -f %04g 0 3
echo {0000..0003} # if bash import sys, pathlib
basedir = pathlib.Path(sys.argv[1])
for i in range(1, 501):
if not (basedir / f'{i:04}_A.csv').is_file():
print(i) for k in $(seq -f %04.f 1 501); do
f="${k}_A.csv"
! [[ -f "$f" ]] && echo $f || :
done
or more succinctly, for k in {0001..0501}_A.csv; do
! [[ -f "$f" ]] && echo $f || :
done
and if you have GNU parallel installed: parallel -kj1 '! [[ -f "{}" ]] && echo {} || :' ::: {0001..0501}_A.csv $ echo -e '0001\n0010\n0002\n0' | bc
1
10
2
0$ printf "%d\n" 003
3
$ printf "%d\n" 009
sh: 1: printf: 009: not completely converted
0I love when programs mysteriously fail when passed an IP address like 192.168.001.009.
Thankfully modern tools have been moving away from supporting that notation and are less likely to explode all over unsuspecting users.
$ printf "%1.0f\n" 009
9
$ printf "%1.0f\n" 00125990
125990
$ printf %d 010
8 ls ????_A.csv | grep -o '[1-9][0-9]*'Then again, Perl is even scarier.
But yeah, the perl rabbit hole goes as deep as you want to, and in some sense it makes it more difficult to say "screw it, I'm redoing this in a real language" as complexity rises. And then you end up with a thousand lines of line noise.
That said the Perl that you write in a one-liner should be different than the Perl that you write when the complexity rises.
If you have more than a few hundred lines, you probably need to move some of it out into a module. There should also be tests for that module. It might even make sense to structure it like a CPAN module so that you can use the existing tools to test and install it.
I wouldn’t wanna use it for more than short scripts. Perl 6 might be fun, but it doesn’t seem to have a large enough community.