6 ms·
Removing leading zeroes doesn't require Python. One easy solution would be sed: $ echo -e '0001\n0010\n0002' | sed 's/^0*//' 1 10 2
by dnet 8y ago
Removing leading zeroes doesn't require Python. One easy solution would be sed:
$ echo -e '0001\n0010\n0002' | sed 's/^0*//'
1
10
2
- v3gas 8y agoOh nice. sed still scares me a bit.
- ianai 8y agoIt’s useful but highly cryptic in my usage.
- SomethingOrNot 8y agoI’m not terribly experienced with Unix tools but I reckon that it might be best to just use Perl instead. Then you just have to worry about PCRE instead of PCRE in addition to old-style regexps. Then again, Perl is even scarier.
- jabl 8y agoperl one-liners are pretty powerful and can replace awk/sed/cut/tr/etc. That being said, then you have to remember which command-line options you should give perl (was it -lane to do X, or was it -pie, or something else entirely?). 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.
- SomethingOrNot 8y agoI haven’t coded Perl beyond one-liners. What attracts me to it are its regular expressions. So much of Unix scripting seems to involve regexps. So I figure Perl+utilities is the better option compared to utilities+Bash. 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.
- b2gills 8y agoIf you want to improve your coding skills you should read “Higher Order Perl”. (made available for free online by the author) If you want to improve your Perl code read “Modern Perl”. (There is more than one version, and I know the first version was made freely available online) Perl is a better language for large codebases than most people give it credit for. That said it allows more creativity when it comes to your code. So you can make awful code just as easily as beautiful code. Perl6 makes the beautiful code easier to write and shorter, while making awful code a bit harder to write.
- chromatic 8y ago> There is more than one version, and I know the first version was made freely available online Good news! Every version is freely available online. Here's the most recent: http://modernperlbooks.com/books/modern_perl_2016/index.html http://modernperlbooks.com/books/modern_perl_2016/index.html
- b2gills 8y agoWell Perl is a real language, so of course it makes it more difficult to say “screw it, I'm redoing this in a real language”. 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.
- jabl 8y agoI have to admit the last time I wrote anything larger than short scripts in perl was in the perl 4 period (on HP-UX to boot). I eagerly awaited the perl 5 version of the Camel book, but in the end I jumped ship to python before getting seriously into perl 5.
- dahart 8y agoDefinitely check out perl one liner patterns. Perl is less scary and more powerful and usually almost as short as sed commands. Perl can often replace pipelines that use both sed and awk. Perl one liners need judicious flags though, common patterns look like perl -ne, perl -pie, perl -lane ... these do very different things. Once you know them, it’s like a minor superpower.
- mshook 8y agoYeah plus seq can generate sequences with leading zeroes (something like seq -f %04.f 1 20). So 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à...
- lelf 8y agoseq -f %04g 0 3 echo {0000..0003} # if bash
- v3gas 8y agoDid not know that. Thanks! That's much easier.
- pwg 8y agoThe seq provided in the GNU toolset has a -w flag to turn on "equal width" mode, so one can also get zero padded numbers out (from GNU seq) by turning on that mode and zero padding the input: $ seq -w 0001 0003 0001 0002 0003 $
- anc84 8y agoNice, this works automatically like this: $ seq -w 98 102 098 099 100 101 102
- neokantian 8y agoYeah, no need for Python. Even the following seems to work fine: $ printf "%d\n" 003 3
- scbrg 8y agoWell, that works up to a point. Fore some, that point might be considered a bit too close to zero... $ printf "%d\n" 009 sh: 1: printf: 009: not completely converted 0
- jandrese 8y ago0-prefix octal notation continues to be a mistake. I 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.
- neokantian 8y agoYes, you are right. This seems to work better: $ printf "%1.0f\n" 009 9 $ printf "%1.0f\n" 00125990 125990
- lelf 8y ago$ printf %d 010 8
- jfk13 8y agoIf one of the items is actually zero, this would delete it entirely, which probably isn't the desired result.
- leblancfg 8y agoYes. Or if you're going to whip out Python, might as well make it all in Python.
- masklinn 8y agoVery much my thinking, especially when there are no significant commands you need to shell out to: 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)
- v3gas 8y agoNice one!
- hellabites 8y agoJust for fun some bash: 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
- jraph 8y ago$ echo -e '0001\n0010\n0002\n0' | bc 1 10 2 0
- hellabites 8y agoThis is better relative to the `sed` solution as it handles the `0000` case well.
- eMSF 8y agoGiven the situation in the article, you might as well do this: ls ????_A.csv | grep -o '[1-9][0-9]*'
- deleted 8y ago[deleted]