Click to See Complete Forum and Search --> : Help with sed and deleting lines


ncsuapex
01-14-2005, 05:16 PM
I am trying to delete lines in a file that has a certain pattern in it. I want to seach the file, for a word or phrase and then have ever line that matches that pattern deleted.

I have tried sed and searched and searched and read through the man pages and ive tried ever possible way to use sed and it's not working. Below are the sed scripts I have tried.

sudo sed -n "pattern /d /g" file
sudo sed -n '/pattern/d' file
sudo sed -n "/pattern/ /d " file
sudo sed -n '/pattern/ /d ' file
sudo sed -n '/pattern/ /d /g' file
sudo sed -e '/pattern/ /d /g' file


I have been able to run the command to print the pattern and it works fine. But when I run the same command but replace the /p with a /d it doesnt work.

sudo sed -n '/pattern/p' file
I am running Debian Sarge. Any ideas on how to get this to work? Or any Ideas on a better way to do what Im trying to do??

bwkaz
01-14-2005, 08:43 PM
First, simplify. ;)

sed -e '/pattern/d' file >~/file2

then ensure that ~/file2 doesn't have the lines in it that match "pattern". You don't need sudo for this, because you're writing to your home directory (unless your normal user doesn't have read permission on the original file, but that's rare).

Then, if that works:

cp file ~/file2
sed -i -e '/pattern/d' ~/file2

to do an inline replace (that's what -i does) on the file in your home directory.

Then, if that works, try:

sudo sed -i -e '/pattern/d' file

to ensure that sudo works.

I'm guessing some step here will fail. The step that fails will probably be indicative of the problem. :)

Oh yeah -- delete ~/file2 when you're done.

ncsuapex
01-15-2005, 08:45 PM
I was able to get it working by just using:
sed -e '/pattern/d' file >~/file2

then moving file2 to file

thanks