Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8660179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:11:34+00:00 2026-06-12T16:11:34+00:00

Newbie here, I’m trying to write to an auto-generated /etc/network/interfaces file of a newely

  • 0

Newbie here,

I’m trying to write to an auto-generated /etc/network/interfaces file of a newely provisioned XEN Ubuntu (12.04/10.04/8.04) DomU server at boot time using (currently) sed.

The auto-generated file is formatted as below:

auto eth0
iface eth0 inet static
    address 192.168.0.88
    gateway 192.168.0.254
    network 255.255.255.255
auto lo
iface lo inet loopback

Using sed, I’m trying to alter lines 1 & 2, add a third line, remove the gateway and last two lines, and append four extra lines at the very end.

I’m currently stuck on adding the third line, as the script adds this line everytime it’s run:

#!/bin/bash

sed -i "1s/.*/auto lo eth0/" /tmp/interfaces
sed -i "2s/.*/iface lo inet loopback/" /tmp/interfaces
sed -i "2a\iface eth0 inet static" /tmp/interfaces
sed -i "s/auto lo//g" /tmp/interfaces

Is it possible to add the third line only if it doesn’t exist using sed (or awk)?

Likewise, how can I delete the gateway and last two lines only if they don’t exist?

I’m new to sed, so am wondering whether I should be looking at awk instead for achieving this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:11:35+00:00Added an answer on June 12, 2026 at 4:11 pm

    You can do that with sed:

    sed -i -e '4{/iface eth0 inet static/! i\
        iface eth0 inet static
        }'
    

    You can group commands with braces. The commands in the braces will only execute on the third line. The i insert command will only execute on the third line and if the third line doesn’t match the string between slashes (the ! after it tells it to execute when it doesn’t match).

    You can do the same to delete:

    sed -i -e '3{/gateway/d}'
    

    Here we delete the third line only if it contains the string gateway. You could probably be more generic and simply do:

    sed -i -e '/gateway/d'
    

    which will delete all lines that contain gateway, but maybe that’s not what you want.

    As for deleting the last lines, the easiest solution would be:

    sed -i -e '${/auto lo/d;/iface lo inet loopback/d}'
    sed -i -e '${/auto lo/d;/iface lo inet loopback/d}'
    

    Where the d delete command is executed on the last line if it matches either auto lo or iface lo inet loopback. Executing it twice will delete the last two lines if they match the patterns.

    If you want to add lines to the end of the file, you can do:

    sed -i -e '$a\
    newline1\
    newline2\
    newline3'
    

    Or maybe only add them if the last line isn’t a specific line:

    sed -i -e '${/192\.168\.1\.1/!a\
    newline1\
    newline2\
    newline3
    }'
    

    Hope this helps a little =)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Newbie here with a frustrating, but probably simple question. I am trying to write
Java newbie here, I am trying to find the output of the following chunk
Javascript newbie here, trying to load Twitter profile images using the URLs returned by
Newbie here. I am trying to store the result of my search onto a
Newbie here...can I write one program which incorporates .NET LINQ and also various Java
awk newbie here.. I'm trying this: top -b -p 30259 | awk 'BEGIN {
Django newbie here stumbling my way around the docs. I'm trying to create a
ES 2.0 newbie here. I'm currently trying to make some 3D interlaced images from
Bit of a Rails newbie here so please bear with me. I am trying
Scala newbie here Trying (1).+(2) returns a Int value of 3, so far so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.