Find the answer to your Linux question:
Results 1 to 5 of 5
Hi all, I want to edit a .conf file on the fly, so I have decided to use sed. The line in focus looks like this Code: server 127.127.25.0 minpoll ...
  1. #1
    Just Joined!
    Join Date
    Sep 2006
    Posts
    22

    Issue with sed

    Hi all,
    I want to edit a .conf file on the fly, so I have decided to use sed.
    The line in focus looks like this
    Code:
    server 127.127.25.0 minpoll 1
    in which I have to replace last 1 with 7 Henceforth sed may be used as
    Code:
    sed -ie 's/^server\(*\)[[:digit:]]/\17/'
    which means
    i) search for a line starting with "server"
    ii) target the last digit
    iii) replace the target with 7
    for which I'm expecting an output like
    Code:
    server 127.127.25.0 minpoll 7
    but what I get is
    Code:
     127.127.25.0 minpoll 7
    can someone help me in this issue

  2. #2
    Linux Newbie birdman's Avatar
    Join Date
    Mar 2006
    Location
    Ireland
    Posts
    141
    You need to put some escaped brackets around server and then output \1\2 before the number 7 (or you could just rewrite server in the replacement field). \1 only refers to what is inside the first set of escaped brackets so does not include server. This is what works on mine:

    Code:
    sed -ie "s/^\(server\)\(.*\)[0-9]/\1\27/" test.conf
    Regards

  3. #3
    Just Joined!
    Join Date
    Sep 2006
    Posts
    22
    Thanks Birdman
    It does work. Is there any system call that can replace this sed, instead calling sed from system().

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Code:
    awk '/^server/{$NF=7}1'

  5. #5
    Linux Newbie birdman's Avatar
    Join Date
    Mar 2006
    Location
    Ireland
    Posts
    141
    What language are you writing in?

    I don't know of any that model sed to allow you to do it as elegantly but post back and someone might be able to help.

Posting Permissions

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