Find the answer to your Linux question:
Results 1 to 3 of 3
I want to replace a line using sed. The problem is that the original line will not have always the same value but will start with a specific word. For ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2012
    Posts
    1

    Sed to replace entire line


    I want to replace a line using sed. The problem is that the original line will not have always the same value but will start with a specific word. For example
    Code:
    domain = example.com #domain will always be domain but example.com will not always be the same
    A command like
    Code:
    sed -i 's/domain = example.com/domain = newdomain.com/' file.txt
    will work only if domain = example.com won't change.
    Any suggestions ?
    Thank you

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,382
    Code:
    sed -e "/^domain = / c\domain = newdomain.com" file.txt
    This will replace *all* lines of file.txt beginning with "domain = ", which might be a bit too strict.
    You maybe want to modify the regex to accept whitespaces.

    Once the sed line does what you want, add the -i switch.
    You must always face the curtain with a bow.

  3. #3
    Linux Enthusiast Mudgen's Avatar
    Join Date
    Feb 2007
    Location
    Virginia
    Posts
    664
    I'd do it
    Code:
    sed -i '.bak' -e"/^domain = .*$/domain = newdomain.com" file.txt/"
    but that's just me. Use -i '' if you don't want a backup, some versions of sed expect input if the -i option is not on the command line.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •