Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Please forgive any errors as this is my first post.
I have a text file that is used to import job details.
each record in the file contains a line in the following format (the * can be any number or letter):
******HSG*********20**************I need a batch program that will find each instance of this line and replace the characters after the "20"
so I end up with the following:******HSG*********20SCDC1234
The characters where I have used the "*" must remain the same.
Can anyone help me as its driving me mad

:: chg lines w HSG and 20
@echo off > newfile & setLocal EnableDelayedExpansion
for /f "tokens=* delims=" %%a in (myfile) do (
set str=%%a
echo !str! | find "HSG" | find "20" > nul
if !errorlevel! equ 0 (set str=!str:20=20SCDC1234!)
echo !str! >> newfile
)
=====================================
If at first you don't succeed, you're about average.M2

Hi M2
Thank you so much for your prompt reply, working through you file I think I almost have it, but one problem is that "20" could appear in any of the spaces I have marked with a "*".If it helps "HSG" will always be the 7,8,9 character in the line and the "20" I am looking for will always be at positions 19 & 20
Sorry I should have made this clearer from the start

:: chg lines w HSG and 20
@echo off > newfile & setLocal EnableDelayedExpansion
for /f "tokens=* delims=" %%a in (myfile) do (
set str=%%a
::echo !str:~6^,2!
if !str:~6^,3! equ HSG if !str:~18^,2! equ 20 (
set str=!str:20=20SCDC1234!
)
echo !str! >> newfile
)
=====================================
If at first you don't succeed, you're about average.M2

Hi M2, you are so near on this I can taste it.
I have run the batch and what is happening is the 20SCDC1234 is being put in place of the existing 20 so I am getting:
******HSG*********20SCDC1234**************
What I need is for the SCDC1234 to replace whatever is after the 20 to get
******HSG*********20SCDC1234
Also I had a line where there was 20 between the HSG and 20 and it inserted the 20SCDC1234 twice.
i.e.
000017HSG03232200020RR010001118999became
000017HSG0323220SCDC12340020SCDC1234RR010001118999
When I need it to be
000017HSG03232200020SCDC1234
I tried to amend your script slightly to achieve this but you really don't want to know what result I got.
Thanks for your help it really is apreciated.

Post a few lines of your actual file so we can get this right.
=====================================
If at first you don't succeed, you're about average.M2

000244HSG0323552000104442
000245HSG032355200020280500480
000246HSG03235520003999 BLAKELAND HILL
000247HSG03235520004DUXFORD
000248HSG03235520005CAMBS
000249HSG03235520006CB25 8RB
000250HSG03235520007Mr J BLOGGS
000251HSG0323552000801223 555666
000252HSG03235520011B028
000253HSG03235520014FIT BANNISTER AND GRAB RAILS
000254HSG03235520015N
000255HSG03235520016JOBR
000256HSG03235520017Y
000257HSG03235520019 215.26
000258HSG03235520020RK050007060999
000259HSG03235520021BOB BUILDER
000260HSG0323552002401223 555666
000261HSG03235520025YYYYYNNYYYYYNNNNNNNNN
000262HSG03235520026MARGARET THATCHER
000263HSG0323552002804/04/2007
000264HSG0323552002902/05/2007 00:00:00
000265HSG03235520031YC116 2.00
000266HSG03235520047AS SHEET IN BAG 03.04.07
000267HSG03235520031YC158 2.00
000268HSG03235520031YC112 1.00
000269HSG03235520047BATHROOM
000270HSG03235520031YC110 1.00
000271HSG03235520047BATHROOM
000272HSG03235520070
000273HSG03235600000HSG
000274HSG0323560000104162
000275HSG032356000020801400530
000276HSG03235600003999 CHASTON ROAD
000277HSG03235600004GREAT SHELFORD
000278HSG03235600005CAMBS
000279HSG03235600006CB28 9AP
000280HSG03235600007Mr D BECKAM
000281HSG03235600008
000282HSG03235600011B080
000283HSG03235600014Supply & fit straight grab rail up to 500mm in
000284HSG03235600014length.
000285HSG03235600015N
000286HSG03235600016JOBR
000287HSG03235600017Y
000288HSG03235600019 93.11
000289HSG03235600020RK050007060999
000290HSG03235600021BOB BUILDER
000291HSG03235600025YYYYYNNYYYYYNNNNNNNNN
000292HSG03235600026POSH SPICE
000293HSG0323560002804/04/2007
000294HSG0323560002902/05/2007 00:00:00
000295HSG03235600031YC110 1.00
000296HSG03235600047ALL WORK AS SHEETS IN BAG 03.04.07
000297HSG03235600031YC110 1.00
000298HSG03235600047OPP W/C
000299HSG03235600031YC112 1.00
000300HSG03235600070Sorry it took so long I had to change names /addresses .
The above consists of two job records.

Call me a stickler for detail but it looks like EVERY line has HSG in positions 7-9. Which makes it pointless to test for it.
=====================================
If at first you don't succeed, you're about average.M2

if you can download stuffs, here's a good tool for string manipulation, gawk for windows(see my sig)
C:\test>gawk "substr($0,19,2)==20{print substr($0,0,20) \"SCDC1234\";next}1" file.txt 000244HSG0323552000104442 000245HSG032355200020280500480 000246HSG03235520003999 BLAKELAND HILL 000247HSG03235520004DUXFORD 000248HSG03235520005CAMBS 000249HSG03235520006CB25 8RB 000250HSG03235520007Mr J BLOGGS 000251HSG0323552000801223 555666 000252HSG03235520011B028 000253HSG03235520014FIT BANNISTER AND GRAB RAILS 000254HSG03235520015N 000255HSG03235520016JOBR 000256HSG03235520017Y 000257HSG03235520019 215.26 000258HSG03235520020SCDC1234 000259HSG03235520021BOB BUILDER 000260HSG0323552002401223 555666 000261HSG03235520025YYYYYNNYYYYYNNNNNNNNN 000262HSG03235520026MARGARET THATCHER 000263HSG0323552002804/04/2007 000264HSG0323552002902/05/2007 00:00:00 000265HSG03235520031YC116 2.00 000266HSG03235520047AS SHEET IN BAG 03.04.07 000267HSG03235520031YC158 2.00 000268HSG03235520031YC112 1.00 000269HSG03235520047BATHROOM 000270HSG03235520031YC110 1.00 000271HSG03235520047BATHROOM 000272HSG03235520070 000273HSG03235600000HSG 000274HSG0323560000104162 000275HSG032356000020801400530 000276HSG03235600003999 CHASTON ROAD 000277HSG03235600004GREAT SHELFORD 000278HSG03235600005CAMBS 000279HSG03235600006CB28 9AP 000280HSG03235600007Mr D BECKAM 000281HSG03235600008 000282HSG03235600011B080 000283HSG03235600014Supply & fit straight grab rail up to 500mm in 000284HSG03235600014length. 000285HSG03235600015N 000286HSG03235600016JOBR 000287HSG03235600017Y 000288HSG03235600019 93.11 000289HSG03235600020SCDC1234 000290HSG03235600021BOB BUILDER 000291HSG03235600025YYYYYNNYYYYYNNNNNNNNN 000292HSG03235600026POSH SPICE 000293HSG0323560002804/04/2007 000294HSG0323560002902/05/2007 00:00:00 000295HSG03235600031YC110 1.00 000296HSG03235600047ALL WORK AS SHEETS IN BAG 03.04.07 000297HSG03235600031YC110 1.00 000298HSG03235600047OPP W/C 000299HSG03235600031YC112 1.00 000300HSG03235600070

Hi M2,
Sorry for the delay in replying I have been away over the weekend, (hope you had a good easter).
yes, you are right every line does have the HSG on it, I suppose I was just concentrating on the constant parts of the line, when really the "20" at characters 19 & 20 is the one I should concentrate on.
Ghostdog - Thanks for the suggestion of Gawk which seems a simple solution, unfortunately I don't think I would be able to install third party software on the server I am hoping to run the batch on

heh, i hope this isnt a windows server -_-;
but anyway: just open up your text editor and replace.
replace: 20**************
with: 20SCDC1234
i dont know how notepad handles wildcards, but in sure you coul use a different editor if need be.I only Batch if possible, 2000 more lines of code, oh well.

BatchFreak, I had already tried this but there could be more than one "20" on a line so I really need to be able to specify that the 20 I am replacing is at position 19 & 20.

So for lines with 20 at pos 19-20 you need to replace 'the rest of the line' with a given string, is that it?
=====================================
If at first you don't succeed, you're about average.M2

I'm headed out the door but I think we're getting traction now.
===============================
:: 14 April 2009 19:48
:: chg lines w 20 at pos 19-20@echo off > newfile & setLocal EnableDelayedExpansion
set /a chgs=0
for /f "tokens=* delims=" %%a in (myfile) do (
set str=%%a
set head=!str:~0^,18!
if !str:~18^,2! equ 20 (
set new=!head!20SCDC1234
echo !new! >> newfile
set /a chgs+=1
) else (
echo !str! >> newfile
)
)
echo !chgs! lines changed
=====================================
If at first you don't succeed, you're about average.M2

M2,
I owe you a drink or three, it works perfectly, I have put it in the test system to run this week and will migrate to live next week.
I cant tell you how much this has helped me.
Many, many thanks

Thanks for letting us know.
=====================================
If at first you don't succeed, you're about average.M2

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |