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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-15-2009, 02:27 AM
|
#1
|
|
Member
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 565
Rep:
|
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
|
|
|
|
01-15-2009, 05:44 AM
|
#2
|
|
Member
Registered: Jul 2006
Distribution: Debian, Ubuntu, openSUSE, CentOS
Posts: 147
Rep:
|
Wrong solution so removed.
Last edited by rizwanrafique; 01-15-2009 at 08:59 AM.
|
|
|
|
01-15-2009, 06:03 AM
|
#3
|
|
Senior Member
Registered: May 2008
Posts: 2,880
|
Quote:
Originally Posted by rizwanrafique
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
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.
|
|
|
|
01-15-2009, 06:14 AM
|
#4
|
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
Maybe this will work for you
Code:
sed -i.bak '{1,24s/.*/# &/; 51,$s/.*/# &/}' file.txt
|
|
|
|
01-15-2009, 08:48 AM
|
#5
|
|
Member
Registered: Jul 2006
Distribution: Debian, Ubuntu, openSUSE, CentOS
Posts: 147
Rep:
|
My mistake. It knows and works well. :-)
|
|
|
|
01-15-2009, 08:52 AM
|
#6
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
You can negate a line range as well:
sed '25-50!s/^/# /' file
This will add # to all lines except for lines 25-50.
|
|
|
|
01-15-2009, 11:05 PM
|
#7
|
|
Member
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 565
Original Poster
Rep:
|
Thanks to All your replies..
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:55 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|