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 10-04-2010, 03:25 AM   #1
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Bangalore, India
Distribution: Linux Mint 13
Posts: 590

Rep: Reputation: 64
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
 
Old 10-04-2010, 03:37 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

sed '$s/::/:DB dept:/' infile

The $ tells sed to only do this for the last line.

Hope this helps.
 
Old 10-04-2010, 03:48 AM   #3
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Bangalore, India
Distribution: Linux Mint 13
Posts: 590

Original Poster
Rep: Reputation: 64
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. :|
 
Old 10-04-2010, 03:58 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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.
Old 10-04-2010, 04:04 AM   #5
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Bangalore, India
Distribution: Linux Mint 13
Posts: 590

Original Poster
Rep: Reputation: 64
Thanks, it worked

What changes I need to make to the command to do the same for a specific line, say line no. 23?
 
Old 10-04-2010, 04:25 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948
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.
 
Old 10-04-2010, 04:32 AM   #7
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Bangalore, India
Distribution: Linux Mint 13
Posts: 590

Original Poster
Rep: Reputation: 64
Thanks for the info david.
 
Old 10-04-2010, 05:00 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by sayan_acharjee View Post
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
 
  


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
bad: Want to insert a line into a text file using "sed" command eliote Linux - General 7 09-19-2010 02:55 AM
Insert a line in the test file with sed say_hi_ravi Programming 16 07-16-2010 10:24 AM
insert text to a file using command line replica88 Linux - Newbie 4 01-28-2010 05:50 PM
How do I insert a line/value after a particular line, in file with sed Glenn D. Programming 3 01-21-2010 09:14 PM
insert a single quote is text file with sed eln01 Programming 7 05-10-2007 11:02 AM


All times are GMT -5. The time now is 11:09 AM.

Main Menu
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