LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Delete a line in a file by script (https://www.linuxquestions.org/questions/linux-general-1/delete-a-line-in-a-file-by-script-577605/)

hraposo 08-16-2007 02:01 PM

Delete a line in a file by script
 
If I in one script to place the line:

echo “deb ftp://ftp.videolan.org/pub/videolan/ubuntu to dapper universe” >> /etc/apt/sources.list

this adds a repository sources.list.

It has some form to erase a line of a file or commenting, by one script?

pixellany 08-16-2007 02:20 PM

Do you mean that you want a command that will delete or modify a line based on content?

SED is the tool I know the best. Suppose you want to delete all lines beginning with "dog":

sed -i '/^dog/ d' filename

Or, add the comment character to all lines containing "dog":

sed -i '/dog/ s/^/#/' filename

Best SED tutorial I have seen (plus other good stuff): http://www.grymoire.com/Unix/

slakmagik 08-16-2007 02:23 PM

Code:

sed -i~ '/address/d'
or
Code:

sed -i~ '/address/s/^/#/'
would be a couple of ways, if I understand your question. If you use this and you're not using gnu sed, you'll need to do the redirect/rename dance.

-- Pixellany beat me to it - fancy formatting slows ya down. :)


All times are GMT -5. The time now is 06:26 PM.