LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Add Line to a File ( 3rd line) (https://www.linuxquestions.org/questions/programming-9/add-line-to-a-file-3rd-line-873416/)

NetRock 04-06-2011 02:23 PM

Add Line to a File ( 3rd line)
 
Hi...
going back to my earlier post "Last digit minus ONE", when i get a Ip addresse with minus 1 ($ip), i need to add the new Ip address ($ip) to resolv.conf of that system.
so from resolve.conf:
Code:

# /etc/resolv.conf .......
domain mydomain.ca
nameserver 11.21.36.98
nameserver 11.21.37.98
nameserver 11.21.38.98

I need to make it like:
Code:

# /etc/resolv.conf
domain mydomain.ca
nameserver $ip
nameserver 11.21.36.98
nameserver 11.21.37.98
nameserver 11.21.38.98
.........  ...........
.........  ...........
.........  ...........

tried with sed

Code:

sed 'nameserver $ip/domain/G' -f /etc/resolv.conf
but not able to get it.

any better way....
thanks

corp769 04-06-2011 02:32 PM

You could do it like so:
Code:

cat /etc/resolv.conf | sed -e '3 i $ip' > /etc/resolv.conf.new
Of course you need to modify that.....
Hope that helps!

Cheers,

Josh

smoker 04-06-2011 03:55 PM

I'm getting fed up with people referring to earlier posts without a link or summary.

Either summarise it or link to it. Don't assume we're following your every word !

corp769 04-06-2011 03:58 PM

Quote:

Originally Posted by smoker (Post 4316307)
I'm getting fed up with people referring to earlier posts without a link or summary.

Either summarise it or link to it. Don't assume we're following your every word !

Who's that to?

Ramurd 04-06-2011 05:16 PM

Quote:

Originally Posted by corp769 (Post 4316313)
Who's that to?

probably your OP

Quote:

going back to my earlier post "Last digit minus ONE",

kurumi 04-06-2011 07:11 PM

Quote:

Originally Posted by NetRock (Post 4316195)

tried with sed

Code:

sed 'nameserver $ip/domain/G' -f /etc/resolv.conf
but not able to get it.

any better way....
thanks


use double quotes so that your $ip variable can be interpolated. Same to some of the answers posted.

NetRock 04-07-2011 07:17 AM

Thank you for your replies.


All times are GMT -5. The time now is 09:00 PM.