4 ms·
Awk really is the tool of choice for this sort of thing: $ awk '{print}/baz/ {sub("baz", "elephant"); print}' jvns.txt foo: - bar - baz - eleph
by davidgould 8y ago
Awk really is the tool of choice for this sort of thing:
$ awk '{print}/baz/ {sub("baz", "elephant"); print}' jvns.txt
foo:
- bar
- baz
- elephant
- bananas
Since the script is single quoted you can also lay it out legibly:
$awk '
{print}
/baz/ {
sub ("baz", "elephant")
print
}
'
which is nice for more complex "one liners".
Awk is also standard on all posix environments and in the mawk flavor is extremely fast (relevant if you are processing huge files).
There is superb book, The AWK Programming Language, which teaches a lot about programming in addition to the awk language. Good discussion and link to pdf here: [0]
[0] https://news.ycombinator.com/item?id=13451454 https://news.ycombinator.com/item?id=13451454
- lucidguppy 8y agoThe AWK Programming Language is the best programming book ever because it lets you learn the language through interesting problems (like writing a very small assembler).
- lemonberry 8y agoIt's available online. Thanks for the tip. https://ia802309.us.archive.org/25/items/pdfy-MgN0H1joIoDVoIC7/The_AWK_Programming_Language.pdf https://ia802309.us.archive.org/25/items/pdfy-MgN0H1joIoDVoI...
- kaushalmodi 8y agoCome on, don't promote piracy of the book! It's worth buying! Mods, please take down this link.
- kaushalmodi 8y agoHmm, someone downvoted me. Can the downvoter explain what was wrong in what I said?
- deleted 8y ago[deleted]
- lemonberry 8y agoI believe the book's been out of print for sometime. I've no idea if that's why you were downvoted. I wouldn't post a link to a pirated anything. That pdf is linked from a ton of sites and the site I found it on seemed like a reputable site, though I don't remember which it was at the moment.
- kaushalmodi 8y ago> I believe the book's been out of print for sometime. Hmm, I did not know that (I have a print copy of that book). > I wouldn't post a link to a pirated anything. OK. But just in case, someone still wants a physical book, they can get it used from Amazon (can talk about Amazon in US) for ~$3.
- wcr3 8y ago(s)ed seems just as good, if not better (depending on size of workload). also, this and most other examples itt don't solve the other part of the problem: saving the changes. ofc, the author provides a solution.
- mjn 8y ago> the mawk flavor is extremely fast Fast, but partly because it's not Unicode-aware: it treats strings as 8-bit character sequences rather than UTF-8. Often that's fine, if non-ASCII characters are only passed through unmodified, but requires some care to avoid problems. $ echo $LANG en_GB.UTF-8 $ echo "ÜNICÖDE" | gawk '{print tolower($0)}' ünicöde $ echo "ÜNICÖDE" | mawk '{print tolower($0)}' �nic�de I ran into this in practice because I was using awk to convert paper titles from "Title Case" to APA-style "Only first word of title capitalized" case. The garbled output led me down a rabbit hole where I discovered that only some awks support Unicode locales, and the default awk on Debian (mawk) isn't one of them.
- davidgould 8y agoSee my post way down the bottom, I do mention this. GNU awk also has a lot of useful extensions and builtins so that sometimes it's painful to use a plain Posix awk. But, when you need the speed, it's nice to know mawk is out there. I'd love to see the mawk compilation technology merged to GNU awk. Or mawk updated with Unicode support and a few of the GNU extensions. The other item on my awk-like wishlist is CSV support, ie split $1 .. $N by CSV rules instead of just a field separator. I usually end up copying CSVs into postgresql because it is fast and then I can process it very flexibly, but it's a bit heavy for things that could be one line-ers. Also, postgresql won't load malformed CSV, but I suspect awk could be less picky. There is miller which hits some of these points, but I haven't really grokked it yet and the syntax seems ... awkward.
- kaushalmodi 8y ago+1 for The AWK Programming language book! I have been reading through it and running code snippets from there as I find time. It's awesome! If anyone's interested, I also update my notes from there on my blog.. https://scripter.co/notes/awk https://scripter.co/notes/awk. I plan to have that notes post contain the entire book when I am done.