LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 09-18-2010, 07:01 AM   #1
eliote
Member
 
Registered: Jun 2010
Distribution: Ubuntu
Posts: 39

Rep: Reputation: 0
bad: Want to insert a line into a text file using "sed" command


Hi all,

I've been reading tutorials of Linux sed command, but haven't got anything yet.
the problem is : I want to insert a line into my DNS database file which has a pattern like below:

<Domain name> 3tabs here <IN> <A> <ip address>

the question is : how to add a line into a file like this using linux sed command? I have problem inserting tabs and the spaces!
could you please help me with the answer?
 
Old 09-18-2010, 07:19 AM   #2
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
Code:
ruby -i.bak -ne 'print "#{$_}insert line\n" if $_=~/Domain name/ or print' file
 
Old 09-18-2010, 07:35 AM   #3
eliote
Member
 
Registered: Jun 2010
Distribution: Ubuntu
Posts: 39

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by kurumi View Post
Code:
ruby -i.bak -ne 'print "#{$_}insert line\n" if $_=~/Domain name/ or print' file
with all respect kurumi, but I'm not really involve with ruby application.
it would be great if you could help me with SED command.
 
Old 09-18-2010, 07:42 AM   #4
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 100Reputation: 100
Code:
$ sed -i '$ a linuxquestions.org\t\t\t<IN> <A> <ip address>' input_file

Last edited by quanta; 09-18-2010 at 07:44 AM.
 
Old 09-18-2010, 08:00 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Please be a bit more specific about what you want to do. What do you mean by "insert" exactly? Do you need the line to go in a specific place? And if so, how can you specify that location? And what kind of text does the file contain, exactly? In other words, what should the before and after files look like?

In particular, sed has two commands, i and a, for insert and append. These are used with address spaces for adding text before or after a line containing a targeted pattern.
Code:
sed '/sometext/i insert this line before sometext' file

sed '/sometext/a insert this line after sometext' file
If you want to insert text into the middle of a line, you need to use the standard s/// command.

Three pages with useful info on sed:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/sed1line.txt
http://sed.sourceforge.net/sedfaq.html
 
Old 09-18-2010, 08:08 AM   #6
eliote
Member
 
Registered: Jun 2010
Distribution: Ubuntu
Posts: 39

Original Poster
Rep: Reputation: 0
thanks quanta,

what if the 'linuxquestion.org' and 'ip address' are the arguments which were asked from the user by a script?
 
Old 09-18-2010, 11:56 AM   #7
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 100Reputation: 100
Code:
quanta@gentoo ~ $ cat input 
JIMMY2    222

quanta@gentoo ~ $ cat test.sh 
#!/bin/bash

echo -n "Enter the domain name: "
read domain_name
ip_address=`nslookup $domain_name | awk '/Address: / { print $2 }'`
sed -i "$ a $domain_name\t\t\t<IN> <A> $ip_address" input

quanta@gentoo ~ $ ./test.sh 
Enter the domain name: linuxquestions.org
quanta@gentoo ~ $ cat input 
JIMMY2    222
linuxquestions.org            <IN> <A> 75.126.162.205
 
Old 09-19-2010, 03:55 AM   #8
eliote
Member
 
Registered: Jun 2010
Distribution: Ubuntu
Posts: 39

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by quanta View Post
Code:
quanta@gentoo ~ $ cat input 
JIMMY2    222

quanta@gentoo ~ $ cat test.sh 
#!/bin/bash

echo -n "Enter the domain name: "
read domain_name
ip_address=`nslookup $domain_name | awk '/Address: / { print $2 }'`
sed -i "$ a $domain_name\t\t\t<IN> <A> $ip_address" input

quanta@gentoo ~ $ ./test.sh 
Enter the domain name: linuxquestions.org
quanta@gentoo ~ $ cat input 
JIMMY2    222
linuxquestions.org            <IN> <A> 75.126.162.205
thank you so much quanta, that worked properly.
 
  


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
sed - use sed to replace multiple instances from "x" to "y" in a line mrodmac Linux - General 4 02-02-2010 12:37 PM
insert text to a file using command line replica88 Linux - Newbie 4 01-28-2010 06:50 PM
SED: how to insert a "&" character in a file carolflb Linux - Newbie 11 12-19-2009 11:48 AM
sed error "command c expects \ followed by text" under OS X (but works in Linux) srunni Programming 3 12-02-2009 03:52 AM
"how do I extract a number from a text file using shell command?" sdandeker Linux - Networking 3 02-12-2004 09:54 AM


All times are GMT -5. The time now is 01:10 PM.

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