Page 1 of 2

Remplace text in middle on line

Posted: Thu May 12, 2011 5:53 am
by Transporter accident
Hello,
Thanks for this wonderful and very useful tool.

I'm totaly not familiar with wild card and other expressions like regex. So please bear with me!

I'll just start off by providing you with an exemple i'm working on:

Multiple batch files containing only one line. here are two batch files for example:

1st file:

%SystemRoot%\explorer.exe /e,\\ctsvmainsaver\Documents\Documents Secteur Atelier Principal\Departements\5159-200 Tonnes\218-5220

2nd file:
%SystemRoot%\explorer.exe /e,\\ctsvmainsaver\Documents\Arborescence Mainsaver\USINE CRABTREE\SF 200 TPD PLANT\159-VENTILATION\404-7070

In both files (and in the other 1397 batch files awaiting me)the text to be replaced always starts from the end of the first '\documents\' and ends at just before the last '\' in the line. The replcement text is the same for both and all the other files: MACHINE-FIBRE-SERVICE\DOCUMENT PAR ACTIF

So here's how the 1st and 2nd files should like after a find and replace:

1st file:

%SystemRoot%\explorer.exe /e,\\ctsvmainsaver\Documents\MACHINE-FIBRE-SERVICE\DOCUMENT PAR ACTIF\218-5220

2nd file:

%SystemRoot%\explorer.exe /e,\\ctsvmainsaver\Documents\MACHINE-FIBRE-SERVICE\DOCUMENT PAR ACTIF\404-7070

How do I configure a regular expression or a Regex to do the job?

MAny thanks in advance for giving me a hand!!

Posted: Thu May 12, 2011 9:55 pm
by Fool4UAnyway
Find:
(\\Documents\\).*(\\[^\\]*)

Replace by:
$1MACHINE-FIBRE-SERVICE\DOCUMENT PAR ACTIF$2

Posted: Tue May 17, 2011 12:58 am
by bh
Certainly beyond me.
I have a similar, but just different enough to stymie me.

I want to change the 3rd number (all numbers will be different) to a zero in every line that ends with 'ABC' 'DEF' 0 1

Thanks very much

Posted: Tue May 17, 2011 11:23 pm
by Fool4UAnyway
I am afraid that is not specific enough for me to come up with the answer.

Could you:
- give an example of one or more lines before and after the requested change?
- tell "in your own words" what exactly you want to happen more completely?

Posted: Wed May 18, 2011 4:16 am
by bh
ok, I will try (without being too long winded about it).
here is a sample of lines in a text file

0 0 9 45 -45 45 -45 45 -45 45 -45
-27.49 0 1.2 'UNDEF' 'UNDEF' 0 1 0
'centre' 'I' 'N' 'N' 0 1
0 0 9 45 -45 45 -45 45 -45 45 -45
-0.5 0.06 0.99 'UNDEF' 'UNDEF' 0 1 0
'right' 'I' 'N' 'N' 0 1
0 0 9 45 -45 45 -45 45 -45 45 -45
26.72 0.02 1.5 'UNDEF' 'UNDEF' 0 1 0

in the second, fifth, and eighth line, I want to change the third numbers (1.2, 0.99, and 1.5) to zero.
These lines are best defined by the the constant 'UNDEF' content in each of the lines in question.
The number to be changed, will always be the 3rd number in the line.
Note that the line numbers will not be consistant from file to file, (eg will not be 2nd, fifth, and eighth).

thanks very much for you help

Posted: Wed May 18, 2011 12:52 pm
by SilentGuy
Assuming that the lines always look the same, you could just replace "-?\d+(\.\d+)? 'UNDEF'" with "0 'UNDEF'".
This does not actually replace the third number but instead replaces any number (with or without decimal point, with or without minus sign) directly in front of a 'UNDEF' with a 0.

If I were to implement it how you phrased it, third number in all lines containing 'UNDEF' 'UNDEF', i would go for this:
"(-?\d+(\.\d+)? -?\d+(\.\d+)?) -?\d+(\.\d+)? (.*'UNDEF' 'UNDEF'.*)"
replaced with "$1 0 $5"

This looks really complex, but this is mainly because of all the optional decimal points and minus signs

Posted: Wed May 18, 2011 3:34 pm
by bh
THANKS !!!
very much appreciated.
I will give this a try, and yes, it DOES look complicated

Posted: Wed May 18, 2011 10:02 pm
by Fool4UAnyway
If this description satisfies the request

"delete the number just before 'Undef'"

then you could use the following regular expression

_[\d\.]+_'UNDEF'

and replace each occurrence by

_0 'UNDEF'

where

_ represents a space character
[\d\.]+ matches any string of one or more characters of the set "0123456789." with the assumption that these will only be numbers with or without decimal point.

Posted: Wed May 18, 2011 10:04 pm
by Fool4UAnyway
If those numbers can be negative as well... Just try to formulate the corresponding expressions yourself... It shouldn't be that hard.

Posted: Thu May 19, 2011 2:47 pm
by bh
Thanks again, but I am clearly missing something. Neither of the suggestions will work for me. I get the "no files found..." message.
When I edit the FIND to just look for UNDEF, it happily finds my files and the lines with this text.
it seems to be the coding that isn't working for me.
Is there another setting somewhere that I need to change. I am using textcrawler 'right out of the box'.

thanks