On Mon, 2009-06-08 at 22:14 -0400, Tony Baldwin wrote: 
> Spiro Harvey wrote:
> >> Can anyone recommend a program/shell script/editor plugin etc, that
> >> can take arbitrary text as input and quote it like email programs
> >> quote emails with a preceding "> " character?
> > 
> > 
> > awk '{print "> " $0}' filename
> > 
> > or just type:
> > 
> > awk '{print "> " $0}' 
> > 
> > paste some text into the buffer, and hit ctrl-D when you're done. It'll
> > return the quoted text which you can paste into your webpage.
> 
> 
> I've been learning to use sed and awk and grep, etc., lately.

Me too, so realize in the following that I don't know all about them.

> I have a general question (probably more appropriate elsewhere, but I'm 
> going to ask here anyway.  Smack me later.).
> 
> 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?

You don't have to generate another file.  You can use shell piping
(named after the pipe character it uses) to redirect the output from sed
into the input of another utility.  An example with awk and grep:

awk '{print "> " $0}' filename | grep 42 

This will email-quote filename and send the results directly into grep,
which will select only any lines with 42 in them. (Windows has a similar
command, but in bash the piping actually runs both programs at the same
time and sends results as they are generated, rather than buffering the
output and sending after the first program ends.)

> If I just do sed -e /searchterm/d file
> I get output to the terminal, but no change to file.

You actually get output to standard output (stdout), which is the
terminal by default but can be redirected with ">output_file" or piped
with "command1 | command2 | command3 | ... | commandN".  This basically
connects the standard output of command1 to the standard input (stdin)
of command2, stdout of command2 to stdin of command3, etc.

> Likewise, I have to the same with grep
> like
> grep -v "^$" filename > newfilename

Again, you don't have to do this.  You probably should read a bash
tutorial, which should further explain piping and other shell tricks.

> I can't just to grep -v "^$" filename
> and have grep do its magic on filename, without generating a second 
> output file.

It's not magic, and there's a good reason you can't.  Unix utilities are
modular: designed to do a simple task well, and to be used with other
utilities for complex tasks.  Piping is one of the most common ways they
are used together.

> (I used both of these today to manipulate a script to remove duplicate 
> files on my system, which I found at 
> http://elonen.iki.fi/code/misc-notes/remove-duplicate-files/ this 
> generated a script that listed all duplicate files with rm at each line, 
> then used sed to remove certain lines from that script, to preserve 
> certain files, used grep to remove the empty lines...just to look nicer).
> It seems fmt does the same, and, I believe awk, as well.
> 
> As such, while manipulating my script today, I kept having to do stuff like
> sed -e "/.gconf/d" script > script2
> mv script2 script
> rm script2
> sed -e "/.java/d" script > script2
> mv script2 script
> rm script2
> 

You could have done

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

(I'm sure there's an even better way to do it by combining the two sed
commands, but I don't know sed well enough to tell.)

> 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*

Again, use piping instead.

> 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.

It wouldn't.  awk and grep and such utilites would be much less
efficient if you couldn't pipe their input/output easily.

> 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.
> 
> be well,
> tony
> 
> -- 
> http://www.photodharma.com
> art / photos / music / tony baldwin
> 
> 

hope this helps,
Brian C. Wells



-- 
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