Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 07-09-2013, 12:52 PM   #1
LQ Newbie
Registered: Mar 2013
Posts: 17

Rep: Reputation: Disabled
Question insert a word in a specific line in a file

I have a file called amer

#cat /tmp/amer

How I insert a word/sentence below the line number 2 ?
I could execute :
# echo "2.2 phone" >> /tmp/amer
but that will append it at the end of the file.

any help?awk and sed will help?
Old 07-09-2013, 01:02 PM   #2
Registered: Mar 2013
Distribution: Redhat, Ubuntu
Posts: 525

Rep: Reputation: 141Reputation: 141
sed -i '3i2.2 phone' /tmp/amer
Edit: Sorry... its
sed -i '3a2.2 phone' /tmp/amer
Edit Again: Damn it... its
sed -i '2a2.2 phone' /tmp/amer
I guess, i should take a break for a while...

Last edited by mddnix; 07-09-2013 at 01:11 PM.
1 members found this post helpful.
Old 07-10-2013, 08:08 AM   #3
Registered: Mar 2013
Posts: 622

Rep: Reputation: 136Reputation: 136
just want to add...
a - insert text after the defined line
sed -i '2aText' file.txt
Add text after the line number 2, it means on number 3
i - insert text at the defined line
sed -i '3iText' file.txt
Add text on the line number 3
1 members found this post helpful.


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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] find specific word in a line in a file. Kashif_Bash Programming 5 04-20-2012 07:15 PM
[SOLVED] Find position of specific word in a line rajeshpvndd Linux - Newbie 3 11-05-2011 08:57 AM
How do I insert a line/value after a particular line, in file with sed Glenn D. Programming 3 01-21-2010 10:14 PM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 02:43 PM
Can not insert word file into mysql table (in blob field) prabhatsoni Linux - Software 2 07-21-2006 06:01 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:56 PM.

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