Message Boards Message Boards

0
|
4205 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

Text File Manipulation

Posted 10 years ago

Hey, I am searching for a possibility to search keywords in a text file and replace the text between this keywords. Basically textfile:

... some text...
tag1originaltexttag2
...sometext

The idea is to search for tag1 and tag2 and substitute the text delimited by these. E.g: with applying a function like function[textfile,"tag1","tag2","newtext"]

... some text...
tag1newtexttag2
...sometext

I have tried several combinations of input output stream manipulations, Find, Position, etc., but didn't find an appropriate solution. Is there an easy function to accomplish this? Thanks in advance

POSTED BY: Jan Dentler
2 Replies
Posted 10 years ago

Can you adapt something like this?

fun[textfile_, tag1_, tag2_, newtext_] := (
   old = Import[textfile]; 
   new = StringReplace[old, RegularExpression[tag1<>".*"<>tag2]->tag1<>newtext<>tag2];
   Export[textfile, new]
   );
fun["textfile.txt", "tag1", "tag2", "newtext"];

You may need to insert the path to your textfile if it isn't in a place that Mathematica knows to look.

POSTED BY: Bill Simpson

A less elegant way. Click the pull down menu "Edit" select "Find" then type in the text you want to change.

POSTED BY: S M Blinder
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract