shadowspar: Side-on picture of a Commodore 64c computer (commodore 64c)
[personal profile] shadowspar posting in [community profile] command_liners
For some reason, I'd gotten the idea into my head that sed(1) is really arcane and difficult to use -- so much so that it wasn't really worth spending the time to learn it. But as I've started finding tiny uses for it, I'm starting to get the impression that it's one of those tools where knowing 10% of its syntax gets you 90% of its benefit.

I often have to run rm -r on a huge tree of thousands of small files. It'd be nice to know how far along things are, but rm -rv shows every file deletion, which is a ton of output -- enough to overwhelm a slow terminal link. Wouldn't it be great if you could print, say, every hundredth line or so?

Enter sed:

rm -rv snapshot/foo | sed -n '0~100p'

sed usually prints every line as it processes it, but -n suspends that -- lines are only printed if you explicitly ask for them with p. And then 0~100 means "operate only on every 100th line". (1~100 would be "line 1, then lines 101, 201, ...".)

Another easy win with sed is being able to feed text through a regex substitution. F'rinstance, lynx -dump URL dumps a web page out as text, with a list of links at the bottom, like this:



If you wanted to download all the animated cat gifs, you'd want to feed that list of links to something like wget, without the leading numbers in the way. But it just so happens that the leading numbers and spaces always add up to exactly six an (admittedly convoluted) way to do that is...

lynx -dump | grep http | grep gif | sed 's/^......//' | wget -nc -i - combination with all the other commands one can string together in a pipeline, I'm sure sed can be turned to a lot of other creative uses!

Date: 2013-04-22 01:50 pm (UTC)
jadelennox: Senora Sabasa Garcia, by Goya (Default)
From: [personal profile] jadelennox
I love sed, and in fact it is one of the things that has slowed down my learning of Perl as a useful commandline tool. I can do so much with wc/cut/sed etc in bash that I never bother to learn commandline Perl. But I also only know a small subset of its functionality, and that "operate only on iterator" tool is amazeballs. Huzzah!

Date: 2013-04-22 10:07 pm (UTC)
kerravonsen: (fractal2)
From: [personal profile] kerravonsen
Yeah, I'm very fond of cut too. It's very simple but useful when one doesn't need the complexity of regular expressions.

But I also only know a small subset of its functionality, and that "operate only on iterator" tool is amazeballs.

(nods) I didn't know about that one either. Now, if I actually remember it by the time I have a use for it, that's another matter. 8-/


command_liners: A command line prompt with a blinking cursor after it, green against black. (Default)
Command Liners

September 2014

1415 1617181920

Most Popular Tags

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 20th, 2017 10:04 pm
Powered by Dreamwidth Studios