Live data from Hacker News

Introduction to sed

catonmat.net

21–30 of 33 posts

Re: Introduction to sed

#21
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…

however, perl has -i, which not all seds do, so those 0.002 seconds you lost to perl will be more than gained by being able to type

      perl -pi -e 's/foo/bar/' somefile
instead of

      sed 's/foo/bar/' somefile > somefile.tmp && mv somefile.tmp somefile

Re: Introduction to sed

#22
post #21
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…

however, perl has -i, which not all seds do, so those 0.002 seconds you lost to perl will be more than gained by being able to type perl -pi -e 's/foo/bar/' somefile instead of sed 's/foo/bar/' somefile > somefile.tmp && mv somefile.tmp somefile

Sed has -i, too! Well, at least GNU sed.

Re: Introduction to sed

#23
post #21

Earlier quoted context omitted.

however, perl has -i, which not all seds do, so those 0.002 seconds you lost to perl will be more than gained by being able to type perl -pi -e 's/foo/bar/' somefile instead of sed 's/foo/bar/' somefile > somefile.tmp && mv somefile.tmp somefile

Sed has -i, too! Well, at least GNU sed.

yes, that's why i said "not all seds".

Re: Introduction to sed

#25
post #21
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…

however, perl has -i, which not all seds do, so those 0.002 seconds you lost to perl will be more than gained by being able to type perl -pi -e 's/foo/bar/' somefile instead of sed 's/foo/bar/' somefile > somefile.tmp && mv somefile.tmp somefile

The utility 'sponge' is useful for doing this in the general case.

Re: Introduction to sed

#26
post #21
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…

however, perl has -i, which not all seds do, so those 0.002 seconds you lost to perl will be more than gained by being able to type perl -pi -e 's/foo/bar/' somefile instead of sed 's/foo/bar/' somefile > somefile.tmp && mv somefile.tmp somefile

Rather than -i alone, I always use -i.bak (or whatever you prefer instead of '.bak'). That way, you get (1) in-place editing and (2) a backup. The files you work on are backed up to "file.bak", so that if you make some simple regex mistake with huge negative consequences, you have a ready fix. (And if you do things perfectly, it's easy to run "rm *bak" after you confirm that.)

Re: Introduction to sed

#28
post #21

Earlier quoted context omitted.

however, perl has -i, which not all seds do, so those 0.002 seconds you lost to perl will be more than gained by being able to type perl -pi -e 's/foo/bar/' somefile instead of sed 's/foo/bar/' somefile > somefile.tmp && mv somefile.tmp somefile

The utility 'sponge' is useful for doing this in the general case.

Part of the 'moreutils' package, for the curious. The description for sponge on the project homepage reads "soak up standard input and write to a file."

Re: Introduction to sed

#29
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…

Apparently the Debian kernel team agrees. I just noticed this while building a kernel package:

test -n "$k" || perl -pli~ -e 's/\$\{shlibs:Depends\}\,?//g' debian/control

Re: Introduction to sed

#30
post #16
post #7

Earlier quoted context omitted.

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.

But the word "World's Best" is still there in the URL, even in the Page Title and the article title. boo !!
Post reply on HN