Re: Looking for quick way of editing a text file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 21Nov2005 00:19, Dave Mitchell <davem@xxxxxxxxx> wrote:
| On Mon, Nov 21, 2005 at 11:06:02AM +1100, Cameron Simpson wrote:
| > Bsed is a wrapper for sed. It takes pretty well the same arguments as sed,
| > but edits files in place. I frequently use it for batch edits; it's very
| > handy. Example:
| > 
| > 	bsed '4s/that/that/' filename.txt
| > 
| > Replace "this" with "that" on line 4. It essentially does all the redirection
| > of the first sed with error handling, temp files and such.
| 
| Note that perl can do a similar edit-in-place, eg
| 
|     perl -ipe 's/this/that/' *.txt

But screws over the file attributes. BTW, you really mean this:

	perl -i -pe 's/this/that/' *.txt

But watch:

	[~]zoob*> echo hello >foo
	[~]zoob*> ls -ldi foo
	6210174 -rw-r--r--  1 cameron cameron 6 Nov 21 11:23 foo
	[~]zoob*> perl -i -pe 's/hell/jell/' foo
	[~]zoob*> ls -ldi foo
	1350276 -rw-r--r--  1 cameron cameron 6 Nov 21 11:25 foo

See? A new file. Had it had hard links, they would now be broken.

And again:

	[~]zoob*1> ls -ldi foo bar
	1350272 lrwxrwxrwx  1 cameron cameron 3 Nov 21 11:27 bar -> foo
	1350276 -rw-r--r--  1 cameron cameron 6 Nov 21 11:25 foo
	[~]zoob*> perl -i -pe 's/jell/hell/' bar 
	[~]zoob*> ls -ldi foo bar               
	1350272 -rw-r--r--  1 cameron cameron 6 Nov 21 11:28 bar
	1350276 -rw-r--r--  1 cameron cameron 6 Nov 21 11:25 foo

Symlink gone, replaced with a new file! (Same inode, freed by the file
remove).

So, perl-i does damage. For this reason I NEVER use it.

Besides, for many things Perl is
overkill.  I tend to use the smallest tool that will succinctly do the
job. So: tr, sed, awk perl/python in that order typically. Or of course
some combination.

Cheers,
-- 
Cameron Simpson <cs@xxxxxxxxxx> DoD#743
http://www.cskk.ezoshosting.com/cs/

I'd climb a mountain for a llama.       - _The Shadow_


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux