Results 1 to 6 of 6

Thread: Replace string in file from file, a bit complicated?

  1. #1
    Join Date
    Aug 2006
    Beans
    Hidden!
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Replace string in file from file, a bit complicated?

    Hi guys,

    When I thought I was almost finished I stumbled upon a problem writing my bash script.

    Let me explain.. I've got two files, newdata.txt and data.xml

    The file newdata.txt looks similar to this:

    <url>http://example.com/1234</url><info>63113</info>
    <url>http://example.com/3253</url><info>245121</info>
    <url>http://example.com/3454</url><info>212512</info>
    <url>http://example.com/6364</url><info>2531</info>
    <url>http://example.com/3582</url><info>2352</info>

    The file data.xml:

    .....
    <url>http://example.com/1234</url>
    .....
    <url>http://example.com/3253</url>
    .....
    <url>http://example.com/3454</url>
    .....
    <url>http://example.com/6364</url>
    .....
    <url>http://example.com/3582</url>
    .....

    What I want to do is replace the line in data.xml with the corresponding data from newdata.txt.

    As you can see from the structure, line by line replacement is not an option.

    My first solution to the problem was something like this.

    1. Read newdata.txt line by line.
    2. Save the line into two variables, $newdata (<url>http://example.com/1234</url><info>2482</info>) and $olddata (<url>http://example.com/1234</url>)
    3. Do a sed replace $olddata with $newdata on data.xml

    And there is the problem, is it even possible to 1. direct data from sed into a variable and 2. use variables with sed?

    Everything I've done up to this point is part of a bash script so I would really appreciate any suggestions on how to achieve this without writing a new program from the beginning.

    Hopefully I made myself clear enough, been sitting with this the entire evening until I crash landed with this problem an hour ago. So I'm a bit nackerd.

    Cheers

  2. #2
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Replace string in file from file, a bit complicated?

    Are the <url>...</url><info>...</info> well formatted? I mean, are there any other things mixed in? Like

    <linkinfo>
    <url>...</url><info>...</info></linkinfo>

    I could write a quick py script.
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  3. #3
    Join Date
    Aug 2006
    Beans
    Hidden!
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: Replace string in file from file, a bit complicated?

    Quote Originally Posted by Can+~ View Post
    Are the <url>...</url><info>...</info> well formatted? I mean, are there any other things mixed in? Like

    <linkinfo>
    <url>...</url><info>...</info></linkinfo>

    I could write a quick py script.
    Oh cheers mate. They are well formated. Really appreciate it!

  4. #4
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Replace string in file from file, a bit complicated?

    PHP Code:
    #!/usr/bin/python

    def getSpaces(line):
        
    """Retrieve the spaces before the first character."""
        
    space ""
        
        
    for char in line:
            if 
    char == " " or char == "\t":
                
    space += char
            
    else:
                break
        
        return 
    space

    fnewdata 
    "newdata.xml"
    fdata "data.xml"
    fexport "data_export.xml" # In case I accidentaly break sth.

    matches = {}

    fnewdata open(fnewdata"r")

    #Open newdata
    for line in fnewdata:
        
    line line.strip()
        
    line_key line
        
        
    #Filter
        
    if line_key.startswith("<url>") and line_key.endswith("</info>"):
            
    #Get the key
            
    line_key line_key.split("<info>")[0]
            
            
    matches[line_key] = line

    fnewdata
    .close()

    fdata open(fdata"r")
    fexport open(fexport"w")

    for 
    line in fdata:
        
    #Swap'em
        
    try:
            
    #Create a correctly formatted string, <space><content>\n
            
    replace "%s%s\n" % (getSpaces(line), matches[line.strip()])
            
            
    #Write it down
            
    fexport.write(replace)    
                
        
    except KeyError:
            
    #Otherwise just copy it
            
    fexport.write(line)

    fdata.close()
    fexport.close() 
    Not the most optimal way, didn't use the xml library or regexps, just a plain if check, so it may not work well.

    It also saves it as another file (data_export.xml) so you can run it safely.

    (whoa, it took me 8 minutes to make this)
    Last edited by Can+~; March 25th, 2009 at 01:58 AM.
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  5. #5
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Replace string in file from file, a bit complicated?

    Code:
    awk 'BEGIN{FS="<[/]?info>"}FNR==NR{_[$1]=$2;next}{print "<url>http://example.com/"_[$1]"</url>"}' newdata data

  6. #6
    Join Date
    Aug 2006
    Beans
    Hidden!
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: Replace string in file from file, a bit complicated?

    Quote Originally Posted by Can+~ View Post
    PHP Code:
    #!/usr/bin/python

    def getSpaces(line):
        
    """Retrieve the spaces before the first character."""
        
    space ""
        
        
    for char in line:
            if 
    char == " " or char == "\t":
                
    space += char
            
    else:
                break
        
        return 
    space

    fnewdata 
    "newdata.xml"
    fdata "data.xml"
    fexport "data_export.xml" # In case I accidentaly break sth.

    matches = {}

    fnewdata open(fnewdata"r")

    #Open newdata
    for line in fnewdata:
        
    line line.strip()
        
    line_key line
        
        
    #Filter
        
    if line_key.startswith("<url>") and line_key.endswith("</info>"):
            
    #Get the key
            
    line_key line_key.split("<info>")[0]
            
            
    matches[line_key] = line

    fnewdata
    .close()

    fdata open(fdata"r")
    fexport open(fexport"w")

    for 
    line in fdata:
        
    #Swap'em
        
    try:
            
    #Create a correctly formatted string, <space><content>\n
            
    replace "%s%s\n" % (getSpaces(line), matches[line.strip()])
            
            
    #Write it down
            
    fexport.write(replace)    
                
        
    except KeyError:
            
    #Otherwise just copy it
            
    fexport.write(line)

    fdata.close()
    fexport.close() 
    Not the most optimal way, didn't use the xml library or regexps, just a plain if check, so it may not work well.

    It also saves it as another file (data_export.xml) so you can run it safely.

    (whoa, it took me 8 minutes to make this)
    Whoa, I love it! Worked like a charm on the first run.
    Thanks!

Bookmarks

Posting Permissions

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