Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, I have a text file which is split into various sections with different names and those sections have the same variables and values defined. eg. [section 1] ipaddr=192.168.1.1 user=user1 ...
  1. #1
    Just Joined!
    Join Date
    Aug 2009
    Location
    Mumbai, India
    Posts
    74

    Specific find and replace



    Hi,

    I have a text file which is split into various sections with different names and those sections have the same variables and values defined. eg.
    [section 1]
    ipaddr=192.168.1.1
    user=user1
    passwd=password1

    [variable 2]
    ipaddr=192.168.1.1
    user=user2
    passwd=password2

    .... and so on...

    I am looking for a command which would help me replace a specific string like the ipaddr value in a specific section - say the second section coz i need to integrate it into a bash script. I don't wanna do a global find and replace.

    Assistance appreciated.

    --Syd

  2. #2
    Just Joined!
    Join Date
    Jan 2007
    Location
    Tamil Nadu, India
    Posts
    18
    If you unique sections, then you can very well use 'sed' command to replace the contents under a particular section.

  3. #3
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by syd05 View Post
    Hi,

    I have a text file which is split into various sections with different names and those sections have the same variables and values defined. eg.
    [section 1]
    ipaddr=192.168.1.1
    user=user1
    passwd=password1

    [variable 2]
    ipaddr=192.168.1.1
    user=user2
    passwd=password2

    .... and so on...

    I am looking for a command which would help me replace a specific string like the ipaddr value in a specific section - say the second section coz i need to integrate it into a bash script. I don't wanna do a global find and replace.

    Assistance appreciated.

    --Syd
    see here (example 3) for similar example

  4. #4
    Just Joined!
    Join Date
    Aug 2009
    Location
    Mumbai, India
    Posts
    74
    Quote Originally Posted by ghostdog74 View Post
    see here (example 3) for similar example
    Hi,

    Thanx for pointing out to an URL. Of course I'll have to brush up on my knowledge of sed since the code mentioned in the URL there did not make much sense to me.

    From whatever little bit i managed to understand i assume the code groups multiple lines of the section and assists with search and replace. In the file that I have, not all the sections are of equal length. I mean some initial sections span 10-15 lines and the latter ones 4 - 5 lines but the sections on which i want the search and replace to work are of similar length.Would the code work in this scenario as well?

    Meantime since the code helps me get started somewhere atleast, I'll get on with understanding sed. Thanx...

    --Syd

Posting Permissions

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