LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 01-15-2009, 02:27 AM   #1
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Rep: Reputation: 30
Insert using sed


Hi all,


Am trying to insert # to the beginning of lines in a file using sed 's/^/#/' and it is working good. How can I avoid inserting # for some lines, say from 25-50 in a file on modifying the same command.

Code:
sed 's/^/#/' filename
Thanks
 
Old 01-15-2009, 05:44 AM   #2
rizwanrafique
Member
 
Registered: Jul 2006
Distribution: Debian, Ubuntu, openSUSE, CentOS
Posts: 147

Rep: Reputation: 19
Wrong solution so removed.

Last edited by rizwanrafique; 01-15-2009 at 08:59 AM.
 
Old 01-15-2009, 06:03 AM   #3
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
Quote:
Originally Posted by rizwanrafique View Post
sed doesn't know about line numbers.
Really?

Code:
bash-3.1$ cat testing
one
two   
three
four
five
six
seven
bash-3.1$ sed '3,6s/^/#/' testing
one
two   
#three
#four
#five
#six
seven
bash-3.1$

Quote:
Originally Posted by ZAMO View Post
How can I avoid inserting # for some lines, say from 25-50 in a file on modifying the same command.

Code:
sed 's/^/#/' filename
Thanks
You can negate the address range, which may suit what you're looking for. Zamo.

Code:
bash-3.1$ sed '3,6!s/^/#/' testing
#one
#two   
three
four
five
six
#seven

Last edited by GazL; 01-15-2009 at 06:23 AM. Reason: added response to zamo.
 
Old 01-15-2009, 06:14 AM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Maybe this will work for you
Code:
sed -i.bak '{1,24s/.*/# &/; 51,$s/.*/# &/}' file.txt
 
Old 01-15-2009, 08:48 AM   #5
rizwanrafique
Member
 
Registered: Jul 2006
Distribution: Debian, Ubuntu, openSUSE, CentOS
Posts: 147

Rep: Reputation: 19
My mistake. It knows and works well. :-)
 
Old 01-15-2009, 08:52 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You can negate a line range as well:
sed '25-50!s/^/# /' file

This will add # to all lines except for lines 25-50.
 
Old 01-15-2009, 11:05 PM   #7
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Original Poster
Rep: Reputation: 30
Thanks to All your replies..
 
  


Reply



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
Insert CRLF's with sed? unSpawn Programming 6 11-22-2006 07:46 AM
SED - replace / insert furquan Programming 5 03-01-2006 06:58 PM
insert string with sed greg108 Programming 7 02-18-2005 01:11 PM
insert a symbol with sed tonton Programming 5 08-31-2004 11:33 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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

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