← Back to team overview

zim-wiki team mailing list archive

Small replace tool for UNIX ( Find and Replace)

 

Hi.

Under UNIX/Linux you may apply a small simple job, see attached tool
Replace.py

To replace occurences of "one two three" in any text files within
the tree /tmp to "three two one" just do:

  find /tmp -name "*.txt" | Replace.py "one two three" "three two one"

and everything is done. You may also catch xml files, svg files and so on,
which would not be covered by zim itself anyway.

In this case you need python and Replace.py has to be executable,
found within your PATH.

Enjoy,

    Andreas



On 04/18/2016 09:59 AM, Jaap Karssenberg wrote:
Hi Karthik,

Afraid this is not possible at the moment. However, since all content in
zim is in text files, you could use another editor with this function if
you nee dit once. If you need it often, makes sense to write a script or
plugin for this.

Regards,

Jaap

On Mon, Apr 18, 2016 at 12:35 AM Karthik Tayur <karthikstayur@xxxxxxxxx
<mailto:karthikstayur@xxxxxxxxx>> wrote:

    Dear All,
    Is there a way I can replace text across pages? The find and replace
    tool only seems to work one page at a time (excluding sub pages).

    Regards
    Karthik Tayur
    _______________________________________________
    Mailing list: https://launchpad.net/~zim-wiki
    Post to     : zim-wiki@xxxxxxxxxxxxxxxxxxx
    <mailto:zim-wiki@xxxxxxxxxxxxxxxxxxx>
    Unsubscribe : https://launchpad.net/~zim-wiki
    More help   : https://help.launchpad.net/ListHelp



_______________________________________________
Mailing list: https://launchpad.net/~zim-wiki
Post to     : zim-wiki@xxxxxxxxxxxxxxxxxxx
Unsubscribe : https://launchpad.net/~zim-wiki
More help   : https://help.launchpad.net/ListHelp

#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-

import os, re, sys

InSitu = 1;   # 1: real job;  0: simulation

def file_replace(fname, s_from, s_to):
    out_fname = fname + ".tmp"
    out       = open(out_fname, "w")
    for line in open(fname):
        out.write(re.sub(s_from, s_to, line))
    out.close()
    if InSitu:
        os.rename(out_fname, fname)
    else:
        print "Simulated: mv %s %s" % (out_fname, fname)


def mass_replace(s_from, s_to):
    for fname in sys.stdin:
        fname = fname.strip()
        file_replace(fname, s_from, s_to)

def main():
    if len(sys.argv) != 3:
        u = "Usage: find . | " + sys.argv[0] + " <string_from> <string_to>\n"
        sys.stderr.write(u)
        sys.exit(1)

    mass_replace(sys.argv[1], sys.argv[2])

if __name__ =="__main__":
    main()


References