1826214 Members
2814 Online
109691 Solutions
New Discussion

need sed help

 
LAFDAL_1
Advisor

need sed help

I want to delete lines from a file, from the first line to the first occurrence of a string XXXXX within the file.
Can you help me to give me the sed right command to do this.
thanks.
Zidane LAFDAL
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor

Re: need sed help

Hi:

By example:

# sed -ne '1,/localhost/d;p' /etc/hosts

Change /localhost/ to the pattern you want to match, and (obviously) substitute your file name for '/etc/hosts'.

Regards!

..JRF...
Tom Ward_1
Honored Contributor

Re: need sed help

Here's another way

sed -n '1,/XXXX/!p' filename

Regards,
Tom
LAFDAL_1
Advisor

Re: need sed help

Thanks,
it works fine.
But how can i do if the string "localhost" is a variable. That is declared as $localhost
Zidane LAFDAL
James R. Ferguson
Acclaimed Contributor

Re: need sed help

Hi:

OK, do this:

# pat=localhost;sed -ne "1,/$pat/!p" /etc/hosts

Note the use of the double quote marks.

Regards!

...JRF...

LAFDAL_1
Advisor

Re: need sed help

Ok thanks
It works fine.
Zidane LAFDAL
Arturo Galbiati
Esteemed Contributor

Re: need sed help

Hi,
just a note: the command
sed -ne "1,/$pat/!p" /etc/hosts
runs fine but doens't remove the entry in the /etc/hosts file. It simply show at the screen the result.
To remove them to have to use:

sed -ne "1,/$pat/!p" /etc/hosts>tt
mv tt /etc/hosts

Rgds,
Art