Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 08-16-2007, 02:01 PM   #1
Registered: Aug 2004
Posts: 370

Rep: Reputation: 30
Delete a line in a file by script

If I in one script to place the line:

echo “deb 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?
Old 08-16-2007, 02:20 PM   #2
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 734Reputation: 734Reputation: 734Reputation: 734Reputation: 734Reputation: 734Reputation: 734
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):
Old 08-16-2007, 02:23 PM   #3
Senior Member
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
sed -i~ '/address/d'
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.

Last edited by slakmagik; 08-16-2007 at 02:24 PM. Reason: redundancy mea culpa



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script to Delete line if pattern exists topcat Programming 22 08-23-2011 04:58 AM
unix shell script:How to delete the first line in a file?? rche3252 Programming 6 03-03-2010 07:32 AM
delete a line containing a pattern and the next line of a text file powah Programming 3 01-31-2007 05:34 PM
delete line specified by variable wihin shel script with sed lnchatterbox Linux - Newbie 2 02-23-2004 01:24 PM
How to delete a line from a text file with shell script programming Bassam General 1 01-28-2004 08:51 PM

All times are GMT -5. The time now is 12:40 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration