Looking for quick way of editing a text file

Cameron Simpson cs at zip.com.au
Mon Nov 21 00:06:02 UTC 2005


On 20Nov2005 15:42, Gilboa Davara <gilboada at netvision.net.il> wrote:
| On Sun, 2005-11-20 at 13:36 +0000, Paul Smith wrote:
| > Is there some quick way of editing a specific line of a text file
| > without using a text editor?
| 
| Try tr(1), sed(1) and awk(1).
| 
| To replace a single line (or part of it):
| cat file.txt | sed 's/old_line/new_line/g' > newfile.txt

1: The superfluous cat. You can write this like so:

	sed 's/old_line/new_line/g' < file.txt > newfile.txt
   
   Very common inefficiency.

2: You might want bsed:

     Page:
     http://www.cskk.ezoshosting.com/cs/css/bsed.html

     Script:
     http://www.cskk.ezoshosting.com/cs/css/bin/bsed

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.

Cheers,
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

Insisting on perfect safety is for people who don't have the balls to live
in the real world.      - Mary Shafer, NASA Ames Dryden




More information about the fedora-list mailing list