Live data from Hacker News

Introduction to sed

catonmat.net

11–20 of 33 posts

Re: Introduction to sed

#12
To quote "The Awful Truth about sed" section of the Grymoire, "It is not your fault you don't understand sed."

I do, however, understand Perl, so that's what I use. E.g.: `echo 'fubar' | perl -lpe 's/fu/foo/'`

It might be a few milliseconds slower-- for example, commenting every line of my 352-line .zshrc (via s/^/#/) takes 0.007s total with Perl (so does `s/^/#/ unless /^\s*#/`) and 0.005s with sed. Commenting out every line of /usr/share/dict/american-english (98569 lines) takes 1.124s with Perl and 0.813s with sed.

Since I already know how to do more complicated things with Perl (like conditionals, named backreferences, etc.) it doesn't seem worth it to take the time to learn how to use sed effectively. I can wait the extra second since I'm not on any kind of deadline or under any efficiency constraints.

I am not trying to say that Perl is better than sed or any other text processing tool. I also don't mean to imply that speed is sed's only advantage-- it's just one example. I think that for someone who already knows some Perl, learning another similar tool doesn't make sense. I'm sure there are exceptions. This is only my humble, personal opinion.

For people who do need/want to learn sed, the article did a pretty good job of showing you how to get a lot done without a whole lot of reading.

Re: Introduction to sed

#13

I dont know if this off topic but is there an utlity that can parse C files and print all the function names or local variable names for example. I know it can be done through sed,but a perfect regex for parsing functions and nothing else can be complex and I am wondering if someone has already done it.

perhaps https://en.wikipedia.org/wiki/Ctags

Re: Introduction to sed

#14
post #13

I dont know if this off topic but is there an utlity that can parse C files and print all the function names or local variable names for example. I know it can be done through sed,but a perfect regex for parsing functions and nothing else can be complex and I am wondering if someone has already done it.

perhaps https://en.wikipedia.org/wiki/Ctags

In particular, `ctags -x` generates human-readable tabular output (instead of the normal output designed for parsing by text editors) and annotates each symbol to indicate whether it's a variable, function, macro, etc.

If you want to get a list of all function names from file "foo.c", you could do something like:

    ctags -x foo.c | awk '$2=="function"{print $1}'

Re: Introduction to sed

#15

I dont know if this off topic but is there an utlity that can parse C files and print all the function names or local variable names for example. I know it can be done through sed,but a perfect regex for parsing functions and nothing else can be complex and I am wondering if someone has already done it.

Look at what the perl script at http://www.gson.org/egypt/ does -- if you are using gcc, you can compile with certain options to generate info from the source code into an intermediate file and then parse this intermediate file with a script to get the info you need. A bit complex, but useful if you are trying to read through code you are maintaining. A compiler is better than a regex for this, I think, for C.

Also ctags/etags like another comment mentioned as also the cscope utility.

Re: Introduction to sed

#16
post #7
post #5

Flagged for excessive self-promotion. Take out "World's best" or make the article provide strong evidence that it's the world's best beyond the fact that its author thinks it is, and I'll unflag it. If this didn't have "World's best" in the blog title and someone had added that to the HN title, a moderator would be deleting those words. The fact that you've inserted the same peacock words in your own blog title as we…

Thanks for letting me know. I just removed the "World's best." I just thought I'd name it "World's best" cause I was feeling fantastic today. :)

Unflagged. Thanks.

Re: Introduction to sed

#17
post #12

To quote "The Awful Truth about sed" section of the Grymoire, "It is not your fault you don't understand sed." I do, however, understand Perl, so that's what I use. E.g.: `echo 'fubar' | perl -lpe 's/fu/foo/'` It might be a few milliseconds slower-- for example, commenting every line of my 352-line .zshrc (via s/^/#/) takes 0.007s total with Perl (so does `s/^/#/ unless /^\s*#/`) and 0.005s with sed. Commenting out e…

On the speed question, I actually find Perl considerably faster than sed in a lot of use-cases, if there's enough processing to dominate the slightly higher startup costs of Perl.

For example, at one point I had reason to take a gigantic single-line textfile, and break it into lines based on a specific 3-letter pattern that didn't occur anywhere else:

   s/ABC/A\nC/g
In whatever sed comes with Debian, this took about 10 minutes, CPU-bound, for a 2-gigabyte file. With Perl: 1.5 minutes, IO-bound. Not too sure why. Maybe sed runs everything through the regex engine, while Perl special-cases constant strings? Perhaps Perl has better buffer management for processing gigabytes of text? I haven't done any real testing.

Re: Introduction to sed

#18
post #12

To quote "The Awful Truth about sed" section of the Grymoire, "It is not your fault you don't understand sed." I do, however, understand Perl, so that's what I use. E.g.: `echo 'fubar' | perl -lpe 's/fu/foo/'` It might be a few milliseconds slower-- for example, commenting every line of my 352-line .zshrc (via s/^/#/) takes 0.007s total with Perl (so does `s/^/#/ unless /^\s*#/`) and 0.005s with sed. Commenting out e…

I know how to do exactly one thing in sed, and that's sed 's/blah/blahprime/' somefile.

If sed has capabilities other than that, I don't particularly care... but that's one thing that I need to do frequently which is more painful in awk or python.

Re: Introduction to sed

#19
post #12

To quote "The Awful Truth about sed" section of the Grymoire, "It is not your fault you don't understand sed." I do, however, understand Perl, so that's what I use. E.g.: `echo 'fubar' | perl -lpe 's/fu/foo/'` It might be a few milliseconds slower-- for example, commenting every line of my 352-line .zshrc (via s/^/#/) takes 0.007s total with Perl (so does `s/^/#/ unless /^\s*#/`) and 0.005s with sed. Commenting out e…

On the speed question, I actually find Perl considerably faster than sed in a lot of use-cases, if there's enough processing to dominate the slightly higher startup costs of Perl. For example, at one point I had reason to take a gigantic single-line textfile, and break it into lines based on a specific 3-letter pattern that didn't occur anywhere else: s/ABC/A\nC/g In whatever sed comes with Debian, this took about 10…

The copyright section of sed(1) suggests that the Debian package sed (I'm running wheezy) is Gnu sed 4.2.1[1]. Plan9 sed is also available in the 9base package. I'm running perl 5.12.4 (also from Debian testing). I don't see any significant difference in speed with vanilla 5.14.1 and the commands I mentioned earlier.

[1] http://www.gnu.org/software/sed/

Re: Introduction to sed

#20
post #18
post #12

To quote "The Awful Truth about sed" section of the Grymoire, "It is not your fault you don't understand sed." I do, however, understand Perl, so that's what I use. E.g.: `echo 'fubar' | perl -lpe 's/fu/foo/'` It might be a few milliseconds slower-- for example, commenting every line of my 352-line .zshrc (via s/^/#/) takes 0.007s total with Perl (so does `s/^/#/ unless /^\s*#/`) and 0.005s with sed. Commenting out e…

I know how to do exactly one thing in sed, and that's sed 's/blah/blahprime/' somefile . If sed has capabilities other than that, I don't particularly care... but that's one thing that I need to do frequently which is more painful in awk or python.

I would guess that that's all a significant number of unix-like OS users do with sed, and in those circumstances, it does make more sense to use sed than a more extensive language. For simple substitutions like that, I'd just use an alias: alias ped='perl -lpe' to save the extra keystrokes.
Post reply on HN