• This topic has 19 replies, 11 voices, and was last updated 6 years ago by ceept.
Viewing 20 posts - 1 through 20 (of 20 total)
  • Help, Multiple find and replace across text (xml) files.
  • seosamh77
    Free Member

    What’s the best way to do this, I have loads of files where I need to change the name of loads text in xml files, the files themselves are named .xdt and .pf.

    Anyhow, I thought I could just do this by opening all the files in notepad++ (v7.5.4) and doing some kind of multiple find and replace. Doesn’t seem an option, in the clickable sense, but

    Google suggests that if I was to do

    Find: (Text1)|(Text2)|(Text3) etc etc etc (I’ll probably have about 50 of these I need to change across multiple files.)

    Replace: (?1TextA)(?1TextB)(?1TextC) etc etc etc

    But that’s not working.

    Any ideas why that isn’t working. Or of another solution? Stuff doing this manually. I’ll be there for weeks.

    whitestone
    Free Member

    It would be easy on a Linux based system! On windows I’d just install Cygwin which is a windows implementation of the Linux shell. Then run this (from memory):

    sed -i ” ‘s/search/replace/g’ ./*.xml

    If you’ve got a lot of search replace pairs then easiest to put them in a file, each search/replace pair on its own line like:

    Fred  Barney

    Call the file changes.txt then a simple loop in a script

    while read -r line

    do

    # split the line (this is – -, two dashes)

    set — ${line}

    # Has to be double quotes around s///g to force variable expansion

    sed -i ” “s/$1/$2/g” ./*.xml

    done < changes.txt

    submarined
    Free Member

    Ah, Notepad++

    As a developer, I’d be buggered without it.

    However, I’m not sure it can do this. Is there any reason you can’t do multiple passes? Iy may appear to take yonks but in reality will probably take just as long as working out a more complex solution.

    Can you write code? It’d be piss easy in c#/vb

    RobHilton
    Free Member

    I’d have thought Notepad++

    But then I’d probly just fudge the arse off it in Excel/VBA as that’s what I tend to do with any quick & dirty bit of work.

    GrahamS
    Full Member

    Anyhow, I thought I could just do this by opening all the files in notepad++ (v7.5.4) and doing some kind of multiple find and replace.

    It definitely lets you replace across multiple files. Go to the “Find In Files” bit in Notepad++. It has a Replace option.

    Or do you mean it doesn’t let you do a complicated replace thing?

    Find: (Text1)|(Text2)|(Text3) etc etc etc (I’ll probably have about 50 of these I need to change across multiple files.)

    Replace: (?1TextA)(?1TextB)(?1TextC) etc etc etc

    Not clear exactly what you are trying to do here? Is this meant to be a mapping? Like Text1 becomes 1TextA, Text2 becomes 1TextB, Text3 becomes 1TextC etc?

    Not sure you can do that in regex.

    Is there any pattern to the change that you want to make? That’s your best bet. Give me some examples and I’ll suggest some regex.

    ebygomm
    Free Member

    You should be able to do what you need to in Notepad ++ there’s a replace in all opened documents option and you can search with regular expressions.

    What exactly do you want to replace?

    Something like this? – appends a to all found filenames

    whitestone
    Free Member

    Get a proper editor like Sublime text (free)

    Go to the folder holding the files then in the menu: Find=>Find in files

    In the search/replace bar put in the text to find and replace then click the three dots at the right hand side which lets you set filters for file types/folders then click replace. You’ll have to do this for each search/replace pair.

    submarined
    Free Member

    Even with regex,I’m pretty sure you can’t build a switch statement for conditional replaces, can you? It’ll only apply to the match.

    submarined
    Free Member

    Whitestone: you can do that in Notepad++ as well, but it sounds like the OP is trying to do it in one pass

    GrahamS
    Full Member

    Get a proper editor like Sublime text (free)

    But you haven’t described anything that can’t be done in Notepad++ (which is very much “a proper editor”).

    TurnerGuy
    Free Member

    Is the xml consistency formatted – if not then a search and replace might not work very well and writing a c# program  to do it, or something to load it into a dom, would be more reliable.

    whitestone
    Free Member

    If you want to do it in one pass (well one user operation) then a script is really the only way to do it. I’ve shown the Bash version. I don’t know Windows Powershell but I’d imagine there’s something similar in there.

    Edit: A search gives this Powershell solution. No idea if it’s valid or not.

    whitestone
    Free Member

    @GrahamS – I’d have added the sarcastic smiley but I don’t have the option to do so in the new forum. I have used Notepad++ BTW

    scaled
    Free Member

    whitestone, i’m very mush a powershell guy, but even I’d do this with sed in WSL 😛

    Powershell replace is klunky at best, especially when you’re trying to do multiples…

    gci -path “PathToFiles” | foreach {

    (Get-Content $file.fullpath) -replace ‘sausage’, ‘bacon’ | Set-Content $file.fullpath

    }

    jimdubleyou
    Full Member

    Notepad++/Search/Find in files.

    You’ll need to do a pass for each thing you want to change.

    whitestone
    Free Member

     i’m very mush a powershell guy, but even I’d do this with sed in WSL

    See my first response

    sharkbait
    Free Member

    I’ve used this for many years and it works perfectly:

    http://www.fauland.com/af9_dl.htm

    Allows you to build up multiple search/replace queries and then apply them to multiple files in one go.

    seosamh77
    Free Member

    I got it btw, notepad++ work, just hadn’t checked the regular expession button at the bottom. (been away testing it.)

    Cheers for all the comments!

    seosamh77
    Free Member

    ie This works fine, just make sure to open all the files and replace across all documents, and make sure regular expressions is checked at the bottom! 🙂

    Find: (Text1)|(Text2)|(Text3) etc etc etc (I’ll probably have about 50 of these I need to change across multiple files.)

    Replace: (?1TextA)(?2TextB)(?3TextC) etc etc etc

    ceept
    Full Member

    I use UltraEdit, It’s fab, but I don’t think it’ll do multiple pairs at one time.

    Personally, I’d stick the search & replace pairs in a CSV file & write a wee VB program to do it, but that’s because I haven’t seen the app that sharkbait suggested. It looks like the tool for the job.

Viewing 20 posts - 1 through 20 (of 20 total)

The topic ‘Help, Multiple find and replace across text (xml) files.’ is closed to new replies.