Wednesday, March 7, 2012

Why sed -n -i is an interesting (?) way to make empty files:

If you're already familiar with the use of sed -i (in-place), as opposed to the normal filtering behavior of sed, then the below will show you why sed -n -i can often result in an empty file.  Enjoy!  It is interesting that one isn't warned about the interaction of -n -i at the command line, but perhaps there is some twisty useful reason to do it that escapes me.

Cheers,
Connie
P.S. The below blog is a great resource on Sed, Perl, Awk, and many other things I haven't needed to do yet.  Check it out!

Famous Sed One-Liners Explained, Part I: File Spacing, Numbering and Text Conversion and Substitution - good coders code, great reuse: "... The only way to make sed output anything with the "-n" switch being on is to use a command that modifies the output stream directly (these commands are '=', 'a', 'c', 'i', 'I', 'p', 'P', 'r' and 'w'). In this one-liner what seems to be the command "$=" is actually a restriction pattern "$" together with the "=" command. The restriction pattern "$" applies the "=" command to the last line only. The "=" command outputs the current line number to standard output. As it is applied to the last line only, this one-liner outputs the number of lines in the file. ..."

'via Blog this'

No comments:

Post a Comment