can anyone help me with a slavish job?

22 views
Skip to first unread message

thegermanguy

unread,
May 25, 2012, 7:34:41 AM5/25/12
to BBEdit Talk
Hi everyone

my boss just came down on me with a slavish task and I was hoping
someone here had an easy (scripting?) solution for it. I've never
written a line of code, so please forgive if my expectations are
unrealistic.

I hava a huge file and need to change things repeatedly in the
following manner:

The lines I need to change ALWAYS follow a line saying

<MTypeName `Index'> (so easy to find)

Then the line I need to change reads for example

<MText `N.: suprascapularis'>

and needs to be changed to

<MText `N. suprascapularis; Nervus suprascapularis'>

i.e.

- delete the colon after in this example the "N."
- insert a semicolon before the " ' "
- repeat the term
- but replace the "N." with "Nervus" (or, if it's a "V." with "Vena"
and there are about 8 different possibilities to be replaced with
different words)

I Imagine for someone who knows what they are doing, this should be a
ridiculously simple task, needing a few lines of code. For me, on
foot, so to say, it'll probably take a week or so.

Could anybody help me out here?

All the best, thegermanguy

Patrick Woolsey

unread,
May 25, 2012, 9:27:14 AM5/25/12
to bbe...@googlegroups.com
thegermanguy <sfx...@googlemail.com> sez:
[...]
>I hava a huge file and need to change things repeatedly in the
>following manner:
>
>The lines I need to change ALWAYS follow a line saying
>
><MTypeName `Index'> (so easy to find)
>
>Then the line I need to change reads for example
>
><MText `N.: suprascapularis'>
>
>and needs to be changed to
>
> <MText `N. suprascapularis; Nervus suprascapularis'>


Two questions about the target file:

Does it contain other instances of these lines:

<MText `N.: suprascapularis'>

that you do *not * want to transform (i.e. which do not follow a line of
"<MTypeName `Index'>") ?

Also, does the term contained within these lines vary, i.e.

<MText `N.: suprascapularis'>
<MText `N.: supraorbital'>
<MText `N.: supernaut'>
...

(I expect so, but would like to make sure. :)


Regards,

Patrick Woolsey
==
Bare Bones Software, Inc. <http://www.barebones.com>
P.O. Box 1048, Bedford, MA 01730-1048

Robert A. Rosenberg

unread,
May 25, 2012, 4:29:30 PM5/25/12
to bbe...@googlegroups.com
At 09:27 -0400 on 05/25/2012, Patrick Woolsey wrote about Re: can
anyone help me with a slavish job?:

>Two questions about the target file:
>
>Does it contain other instances of these lines:
>
><MText `N.: suprascapularis'>
>
>that you do *not * want to transform (i.e. which do not follow a line of
>"<MTypeName `Index'>") ?


That can be handled by first removing the new line character after
<MTypeName `Index'> so it becomes <MTypeName `Index'><MText ... and
then do the compare. After that is done, just globally add the new
line back in.

Matt Martini

unread,
May 31, 2012, 12:10:40 PM5/31/12
to bbe...@googlegroups.com
thegermanguy,

You can use this simple perl program to make your changes.

To use it, in Terminal do the following:
$ cat OLDFILE | perl slavish.pl > NEWFILE

This will make a copy of OLDFILE called NEWFILE with the changes you desire (while preserving the original).
This is a simple program, which could be optimized and have more error checking, yet it will do the job and 
is pretty straightforward to read.

You will need to modify it for your other conversion variations, this is marked and should be easy to follow.

Good Luck 

Matt

_______________________________________________________________________
#!/usr/bin/perl

$index_line = q[<MTypeName `Index'>];

while ( $line = <> ) {

    if ( $line =~ m|$index_line| ) {
        $change_line = <>;
        $change_line =~ s|<MText `N\.: (.*)'>|<MText `N. $1; Nervus $1'>|;
        $change_line =~ s|<MText `V\.: (.*)'>|<MText `V. $1; Vena $1'>|;

        ### Add in the other variations here, remove the # at the beginning of the line
    #   $change_line =~ s|<MText `X\.: (.*)'>|<MText `X. \1; XRAY \1'>|;
    #   $change_line =~ s|<MText `Y\.: (.*)'>|<MText `Y. \1; YANKEE \1'>|;
    #   $change_line =~ s|<MText `Z\.: (.*)'>|<MText `Z. \1; ZULU \1'>|;

        print $line;
        print $change_line;
    } else {
        print $line;
    }
}

_______________________________________________________________________

Test File:

I hava a huge file and need to change things repeatedly in the
following manner:
The lines I need to change ALWAYS follow a line saying
<MTypeName `Index'>
<MText `N.: suprascapularis'>
more stuff
<MTypeName `Index'>
<MText `V.: supraorbital'>
even more stuff
<MTypeName `Index'>
<MText `N.: supernaut'>
still more stuff

_______________________________________________________________________


Reply all
Reply to author
Forward
0 new messages