LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 07-09-2013, 12:52 PM   #1
AhmedAmer12
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
1-name
2-age
3-edu
4-email

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
mddnix
Member
 
Registered: Mar 2013
Distribution: Redhat, Ubuntu
Posts: 516

Rep: Reputation: 139Reputation: 139
Code:
sed -i '3i2.2 phone' /tmp/amer
Edit: Sorry... its
Code:
sed -i '3a2.2 phone' /tmp/amer
Edit Again: Damn it... its
Code:
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
eklavya
Member
 
Registered: Mar 2013
Posts: 619

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


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


All times are GMT -5. The time now is 07:33 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration