LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   bad: Want to insert a line into a text file using "sed" command (https://www.linuxquestions.org/questions/linux-general-1/bad-want-to-insert-a-line-into-a-text-file-using-sed-command-833006/)

eliote 09-18-2010 06:01 AM

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?

kurumi 09-18-2010 06:19 AM

Code:

ruby -i.bak -ne 'print "#{$_}insert line\n" if $_=~/Domain name/ or print' file

eliote 09-18-2010 06:35 AM

Quote:

Originally Posted by kurumi (Post 4101581)
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.

quanta 09-18-2010 06:42 AM

Code:

$ sed -i '$ a linuxquestions.org\t\t\t<IN> <A> <ip address>' input_file

David the H. 09-18-2010 07:00 AM

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

eliote 09-18-2010 07:08 AM

thanks quanta,

what if the 'linuxquestion.org' and 'ip address' are the arguments which were asked from the user by a script?

quanta 09-18-2010 10:56 AM

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


eliote 09-19-2010 02:55 AM

Quote:

Originally Posted by quanta (Post 4101818)
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.


All times are GMT -5. The time now is 04:52 AM.