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

Re: sed/awk/fmt question hijacked from Re: Program for quoting text like in email?



Tony Baldwin <photodharma@gmail.com> writes:

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

GNU sed (which is what you are most likely running) has the -i option to
make the changes in the source file.

sed -i -e /searchterm/d inoutfile

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

You cannot do the same with grep, since grep is usually used to print
out pattern matches. You would not normally want to kill the file you
are grepping.

In your specific case, you can use sed:

sed -i -e "/^$/d" filename

>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 can combine these into a single sed invocation:

set -i -e "/.gconf/d" -e "/.java/d" script


Reply to: