Find the answer to your Linux question:
Results 1 to 5 of 5
Hi guys, To give you a bit of background i have the task of removing the Source Integrity comments from all text files (*.java, *.xml, *. js ) for our ...
  1. #1
    Just Joined!
    Join Date
    Aug 2008
    Posts
    2

    Multi-pattern find & replace

    Hi guys,

    To give you a bit of background i have the task of removing the Source Integrity comments from all text files (*.java, *.xml, *.js) for our move to SVN. About time! The structure of the files is as follows

    ...
    <!--
    $Log: SchemaToCastorMapping.xsl $

    ....
    -->
    ...

    I initially assumed that this would always appear at the end of the file & the solution was the following function which loops through all xml files with the text "$Log:" and remove all text until "-->". I then appended --> to the end of the file.:

    function removeXML {
    echo "Processing filetypes : *.xml started"
    find . -name "*.xml" -exec grep -l "\$Log:" {} \; | while read f
    do
    echo "Removing from : " $f
    cp $f tmp.tmp
    chmod +w $f
    cat tmp.tmp | sed '/$Log:/,/-->/ d' > $f
    rm tmp.tmp
    echo '-->' >> $f
    done
    echo "Processing filetypes : *.xml Compete"
    }

    However given that the log comments can appear anywhere in the file I now need to do a sed pattern find & replace instead of a sed pattern delete.

    Can anyone suggest the sed command i need to do this. Instead of using the delete action on the sed parrten i wish to replace with "-->".

    Thanks

    Rich

  2. #2
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    As long as the comments start and end on a new line, I don't see your problem. The delete pattern will still work.

  3. #3
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Even if not, see here

  4. #4
    Just Joined!
    Join Date
    Aug 2008
    Posts
    2
    Hi burschik,

    Thanks for your reply. The problem i have is that i now want to convert the delete to a replace so any instances of my pattern search will be replaced with "-->" insteard of just deleting the block?

    Can you provide me an example of the sed command to do this?

    Thanks

    Rich

  5. #5
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    The last line of the script I linked to is a replace command. It replaces anything in SGML comment markers with nothing. You could easily replace it with something else.

Posting Permissions

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