LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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, 03:27 AM   #1
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 579

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, 06: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 09:59 AM.
 
Old 01-15-2009, 07:03 AM   #3
GazL
Senior Member
 
Registered: May 2008
Posts: 3,502

Rep: Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024
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 07:23 AM. Reason: added response to zamo.
 
Old 01-15-2009, 07:14 AM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Maybe this will work for you
Code:
sed -i.bak '{1,24s/.*/# &/; 51,$s/.*/# &/}' file.txt
 
Old 01-15-2009, 09: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, 09:52 AM   #6
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
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-16-2009, 12:05 AM   #7
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 579

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


Reply


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


All times are GMT -5. The time now is 09:09 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration