Introduction to sed
catonmat.net
catonmat.net
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 well as your HN submission does not make this any better.
(I don't like being so blunt with a highly regarded veteran HN user, but abuse is abuse whoever does it. Sorry, Peteris.)
[EDITED to add: The HN guidelines say not to comment saying "I flagged this". I think this is an unusual case. I appreciate that others may disagree.]
Perhaps more useful for an introduction would be retaining portions of the input back-references (\1 to \9) and `&'. Also useful is is the `g' flag for the `s' command, for global replacement, and the `i' flag (a GNU extension) for case insensitivity. Another note is that you can actually use any character to delimit `s' commands you want, which is useful if you have /'s in your pattern or replacement strings. The last main part of sed which I use regularly is the -r option, for extended regular expressions (basically the same as grep), which lets you use regular expression tokens like + and | without escaping. On that note, the author misses this point in his introduction, and the example of "sed -n '/a+b+/p'" should actually be either "sed -rn '/a+b+/p'" or "sed -n '/a\+b\+/p'".
For a serious introduction to all the extra functionality in sed, there's a great reference at the top google result for sed[1]. In addition, GNU sed adds functionality that you might be useful at some point (e.g. s///e), all of which are described in the GNU sed user's manual[2].
[1] http://www.grymoire.com/Unix/Sed.html [2] http://www.gnu.org/software/sed/manual/index.html
I have two e-books, one on Awk, and the other on Sed. Here are the threads of the e-books on HN:
Awk: http://news.ycombinator.com/item?id=2674284
Sed: http://news.ycombinator.com/item?id=2995711
And here are reviews that people have sent me:
Awk e-book:
http://www.johndcook.com/blog/2011/08/31/awk-one-liners/
https://plus.google.com/102155999275405024037/posts/W5ANwQgV...
http://benoithamelin.tumblr.com/post/10559496046/review-awk-...
http://twitter.com/#!/barryezl/status/121334247893114882
Sed e-book:
Having said that, if you're a long beard who has long been acquainted with these tools, you could maybe skip them.
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.
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.GNU stuff gets a speed improvement from assuming single byte characters.
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.
perl -pi -e 's/foo/bar/' somefile
instead of sed 's/foo/bar/' somefile > somefile.tmp && mv somefile.tmp somefiletest -n "$k" || perl -pli~ -e 's/\$\{shlibs:Depends\}\,?//g' debian/control
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.
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}'Also ctags/etags like another comment mentioned as also the cscope utility.
s/&/&/g
Sorry! :D