Re: Parsing

new topic     » goto parent     » topic index » view thread      » older message » newer message

Greg Phillips wrote:

>I'm looking for a simple way to read from a text file, replacing
>certain strings with others.
>
>For example: Hello there, my name is [Name].

>[Name] would be replaced with "Greg".
>
>There is an emphsis an accuracy, and simplicity.  There *must* be a
>simpler way than how I'm doing it.


Greg,

The attached command line utility is not much different from Junko's
tool. Sort of a subset, you might say. It has just one advantage, I
can think of, it allows you to search for and replace even strings
containing new lines, etc, because it handles input/output files as
binaries.

It may be faster, because it handles the whole file in a single buffer
as against so many lines, but on the other hand, it may be slower,
because bigger slices may take longer to manipulate. I just do not
know, I have not conducted any speed tests.

Enjoy. jiri


-- <snip> ------------------------------------------------------------

--  file    : replace.ex
--  author  : jiri babor
--  email   : jbabor at paradise.net.nz
--  project : search & replace
--  tool    : euphoria 2.1
--  date    : 99-11-25
--  version : 1.00

----------------------------------------------------------------------
--  Usage: ex  replace  old_text  new_text  file1  file2  file3 ... --
----------------------------------------------------------------------
--  Replace all occurances of old_text string with new_text string  --
--  in all specified files.                                         --
--  Strings containing spaces must be enclosed in quotation marks!  --
----------------------------------------------------------------------
--  **************  Play safe! Back up your files!  **************  --
----------------------------------------------------------------------

include file.e

sequence buffer, cl, files, new_text, old_text

procedure help(sequence error_message)
    puts(1,"Error :  " & error_message & "\n")
    puts(1,"Syntax:  ex  replace  old_text  new_text  file1  file2 ...\n")
    puts(1,"Note  :  enclose text containing spaces in quotation marks.\n")
    abort(1)
end procedure

procedure read_file(sequence filename)
    integer f, len, n

    f=open(filename,"rb")
    if f=-1 then
        help("Couldn't open " & filename & " !\n")
    end if
    n=seek(f, -1)               -- go to end of input file
    len=where(f)                -- get length of input file in bytes
    n=seek(f, 0)                -- go back to beginning of input file
    buffer = repeat(0, len)     -- init buffer
    for i=1 to len do
        buffer[i] = getc(f)     -- read file into buffer
    end for
    close(f)
end procedure -- read_file

procedure replace()             -- basically same as Junko's
    integer i, j, lo, ln

    lo = length(old_text)
    ln = length(new_text)

    i = 0
    j = match(old_text, buffer)
    while j do
        j += i
        buffer = buffer[1..j-1] & new_text & buffer[j+lo..length(buffer)]
        i = j+ln-1
        j = match(old_text, buffer[i+1..length(buffer)])
    end while
end procedure

procedure write_file(sequence filename)
    integer f

    f=open(filename,"wb")       -- open output file
    puts(f, buffer)             -- write out
    close(f)                    -- close output file
end procedure -- write_file

-- main ------------------------------------------------------------------------

cl=command_line()
if length(cl) < 5 then
    help("Insufficient number of arguments!")
end if

old_text = cl[3]
new_text = cl[4]
files = cl[5..length(cl)]

for i=1 to length(files) do
    read_file(files[i])
    replace()
    write_file(files[i])
end for

puts(1, "Done!\n")

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu