Message Boards Message Boards

GROUPS:

Text File Manipulation

Posted 8 years ago
3079 Views
|
2 Replies
|
0 Total Likes
|

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

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
Posted 8 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
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