3 ms·
Have you ever done string manipulation (also RegEx) over many files in Perl? I think it's in this regard superior than anything else, even python when looking a
by therealmarv 9y ago
Have you ever done string manipulation (also RegEx) over many files in Perl? I think it's in this regard superior than anything else, even python when looking at syntax.
- Outpox 9y agoFor someone who hasn't done any Perl yet and only little Python could you explain why?
- mfontani 9y agoFor the specific thing the OP mentioned, this: $ perl -pi -e 's/MY_NAME/Marco/g' ./*.txt ... would go through all "*.txt" files in the directory, and globally replace the token "MY_NAME" with "Marco". Great for a {poor man's,simple and easy to understand} basic template system to be used inside a bash script ;)
- JetSpiegel 9y agoOverkill. Just use: $ sed -i 's,MY_NAME,Marco,g' *.txt
- therealmarv 9y agonope. I've dealt recently with some big DB files (I think GB size) and regex doing the same. sed is a lot slower than perl for this kind of task.
- jstimpfle 9y agoI don't know how sed and perl compare, but I know these things: Use a C local (e.g. LANG=C or LC_ALL=C) for dramatic speed up of tools like grep/sort and probably also sed. If you don't do that, usually the UTF-8 parser kicks in, which is a lot slower. Perl is actually well known to have a non-optimal regex implementation: https://swtch.com/~rsc/regexp/regexp1.html https://swtch.com/~rsc/regexp/regexp1.html That said, I don't know how fast it is (what optimizations it has) at simple string substitutions.
- arjie 9y agoI have not. I usually use `sed` in such instances but admitting the superiority of Perl in such instances surely this is insufficient on its own to use Perl? Seems like a rare use-case and if that script grows large then Python has superior language features but if the script stays small, then a Bash script with `sed` will do just as well.