On Seg, 08 Jun 2009, Tony Baldwin wrote:
I've been learning to use sed and awk and grep, etc., lately.
I have a general question (probably more appropriate elsewhere, but I'm going to ask here anyway. Smack me later.).

Bad, very bad.

Why is it that with sed, stuff like
sed -e /searchterm/d
I have to do
sed -e /searchterm/d infile > outfile,
and can't just do sed -e /searchterm/d file, without having to generate another file?
If I just do sed -e /searchterm/d file
I get output to the terminal, but no change to file.

That's how UNIX works. Programs such as sed, grep, fmt, cut, head, tail, etc. are filters: they read input (from stdin or a file), do something with it, and output (generally to stdout, which can be redirected by the shell).

While in some cases in place editing is better and easier (and some utilities have that feature, such as sed's -i option), this way is more versatile, and allows for simpler utilities: they only have to do the job they are designed to do, not worry about renaming files, deleting the old copy, writing new files, etc.) And it comes in handy in the situation you want below, as we will see.

I was trying to use sed to manipulate the script rather than just open it in a text editor and edit it by hand, but, in the long run, it didn't come out much more efficient...
I suppose had I done

sed -e "/.gconf/d" script > script0
sed -e "/.java/d" script0 > script1
sed -e "/.adobe/d" script1 > script2
sed -e "/docs\/.evilplans\/todo/d" script2 > script3
(etc.)
sed -e "/finalsearchterm/d" script25 > MyFinalScript
rm script*

maybe I could have further automated this, but, it just seems that if sed would work on a target file, without have to be ">"ed to an output file, that it would be more efficient. I mean, in the above case, I end up with 25 scripts before I'm done manipulating the text. I feel like I must be missing something really essential here, because I imagine this could be done more efficiently. Sorry if this is the wrong place to ask, but I'm just really an infant with bash scripting, eager to learn, and following some of these threads has been enlightening.

You can pipe the output of a command into another command:

sed -e "/.gconf/d" script | sed -e "/.java/d" | sed -e "/.adobe/d" | ........ |
sed -e "/finalsearchterm/d" > MyFinalScript

In the case of sed, there is probably a way to do several edits with only one invocation, but this pattern is useful when combining different commands. I personally think this is one of the reasons UNIX-like systems are so powerful and allow you to work much more efficiently than one does under Windows.


--
Eduardo M KALINOWSKI
edua...@kalinowski.com.br


--
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org

Reply via email to