4 ms·
Worth it just for learning that there's a "replace" command, for multi-file search-and-replace without having to remember sed syntax. Can't believe I've been r
by camtarn 10y ago
Worth it just for learning that there's a "replace" command, for multi-file search-and-replace without having to remember sed syntax.
Can't believe I've been running Linux for 16 years and I didn't know about that one.
- gizmo686 10y agoOn my system (Ubuntu 16.04) replace is not installed by default. It appears to be provided by the following packages: mysql-server-5.7 mariadb-server-10.0 percona-server-server-5.6 percona-xtradb-cluster-server-5.6 I downloaded mysql to check, and it appears that the replace provided is as described in the article; I assume the others are as well.
- voltagex_ 10y agoI'm not sure how I feel about installing something as big as mysql-server just to get one utility. Unfortunately it seems to pull in some MySQL/MariaDB specific code - https://github.com/MariaDB/server/blob/bb2c1a52c61706dde8c525a8887f2d364c0db1eb/extra/replace.c https://github.com/MariaDB/server/blob/bb2c1a52c61706dde8c52...
- deleted 10y ago[deleted]
- deleted 10y ago[deleted]
- Sophira 10y agoIt's also worth noting that in MySQL, the replace command is deprecated in 5.7.18 and removed in MySQL 8. [0] I don't know its status in MariaDB. [0] https://dev.mysql.com/doc/refman/5.7/en/replace-utility.html https://dev.mysql.com/doc/refman/5.7/en/replace-utility.html
- kevhito 10y agoI was tempted to poke a little fun at you for having trouble with sed -- among my peers, 's/foo/bar/g' used pretty much daily, even just in informal emails/conversations. But I'm glad I looked at the list first, because I found this wonder: `lstopo --of txt`. I'm going to use this in class next week, I can't believe I've never seen that before.
- linkregister 10y agoMy biggest hardship with sed is having to figure out how to escape the operators within regular expressions on the command line. I think it's pretty reasonable to spend 15+ minutes on a complicated replacement regex. Edit: It looks like I agree with you; `replace` only solves trivial problems that can easily be done with `sed`.
- userbinator 10y agoYou can use single quotes to escape everything except single quotes from the shell. Then use backslashes to escape single quotes where needed. If you haven't taken the time, I'd say it's well worth learning exactly how the shell escapes work. It's surprisingly simple and natural once you get used to it.
- linkregister 10y agoThanks for the advice; I should block some time to fully grok shell escapes. Even with single quotes, sed still requires escaping parentheses, + operators, and other operators that would otherwise be interpreted literally (but not all operators should be escaped). In the languages that I learned regexes this wasn't required.
- NathanOsullivan 10y agoThere's a command line switch -E for sed that gives you what we think of as "normal" regex escaping.
- aeosynth 10y agoNot covered in the article, but `rename` is a multi-file renamer. I wrote a script which used an `echo $f | sed | mv` loop before I learned about this.
- my_ghola 10y agoBe careful there's at least two implementations of rename in different Linux distros. I've run into one made in perl that uses regex in Debian (e.g. `rename 's/foo/bar/' * `). And another one that uses simple strings (`rename foo bar * `) in CentOS.
- bartl 10y agoIndeed. The Perl `rename` is based on the module [File::Rename](http://search.cpan.org/~rmbarker/File-Rename-0.20/ http://search.cpan.org/~rmbarker/File-Rename-0.20/). This name clash makes that the perl module cannot be easily used on CentOS, since the CentOS utility is required for building RPM packages. You'd have to edit Makefile.PL of the Perl module before installation, to avoid that clash. (I'd prefer to keep the name as "rename.pl", in this case; though file-rename, as on Windows, would work too.)
- leni536 10y agoI use moreutils' vidir for renaming, I don't know rename though.
- bowmessage 10y agorpl is pretty nice as well.
- scrollaway 10y agoI was super confused at your comment. Search on Arch Linux packages, it looks like the replace command is shipped with mysql/mariadb. ?!