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. |
|
 |
10-04-2010, 03:25 AM
|
#1
|
|
Member
Registered: Feb 2010
Location: Bangalore, India
Distribution: Fedora, Linux Mint 10
Posts: 588
Rep:
|
trying to insert text in the last line of a file with sed command....
I want to insert the text "DB dept" in the last line of passwd file which looks like this right now
Quote:
|
newuser:x:717:717::/home/sacharje:/bin/bash
|
And I want it to be like this:
Quote:
|
newuser:x:717:717: DB dept:/home/sacharje:/bin/bash
|
I tried to do that with sed but failed, I'd like to know the proper syntex with sed to achieve this, kindly help.
Thanks in advance 
|
|
|
|
10-04-2010, 03:37 AM
|
#2
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,866
|
Hi,
sed '$s/::/:DB dept:/' infile
The $ tells sed to only do this for the last line.
Hope this helps.
|
|
|
|
10-04-2010, 03:48 AM
|
#3
|
|
Member
Registered: Feb 2010
Location: Bangalore, India
Distribution: Fedora, Linux Mint 10
Posts: 588
Original Poster
Rep:
|
When I am using that command it shows that it worked but now when I open the file again I don't see the change. :|
|
|
|
|
10-04-2010, 03:58 AM
|
#4
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Code:
awk -F":" '{print p}{p=$0}END{$5="DB dept"$5;print}' OFS=":" /etc/passwd > t && mv t /etc/passwd
make sure you are root.
|
|
|
1 members found this post helpful.
|
10-04-2010, 04:04 AM
|
#5
|
|
Member
Registered: Feb 2010
Location: Bangalore, India
Distribution: Fedora, Linux Mint 10
Posts: 588
Original Poster
Rep:
|
Thanks, it worked
What changes I need to make to the command to do the same for a specific line, say line no. 23?
|
|
|
|
10-04-2010, 04:25 AM
|
#6
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,697
|
Like most applications of this type, sed doesn't modify the file itself by default, but you can add the -i option to do so.
The basic format of a sed command is this:
sed [options] (-e)[address range][expression] (-e [address range][expression])
The address range tells sed which lines to operate on. It can be a single number for one line, or number1,number2 for a range of lines. There's also $ to mean the last line and a few more modifiers.
Code:
sed -i '$ s/a/b/g' #changes all a's to b's on the last line of the file,
#modifying the original.
sed '5,7 d' #deletes lines 5-7, but only in the screen output.
sed -n '5,7p' #prints only lines 5-7 from the file.
sed '10 a foobar' #adds "foobar" after line 10.
sed can do a lot more than most people realize.
Here are a few useful references:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt
Last edited by David the H.; 10-04-2010 at 04:27 AM.
|
|
|
|
10-04-2010, 04:32 AM
|
#7
|
|
Member
Registered: Feb 2010
Location: Bangalore, India
Distribution: Fedora, Linux Mint 10
Posts: 588
Original Poster
Rep:
|
Thanks for the info david. 
|
|
|
|
10-04-2010, 05:00 AM
|
#8
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Quote:
Originally Posted by sayan_acharjee
Thanks, it worked
What changes I need to make to the command to do the same for a specific line, say line no. 23?
|
Code:
awk 'NR==23 { ........ }' file
|
|
|
|
| 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 03:14 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
|
|