Solved Replace and inserting new lines in text file

Dell / Optiplex 960
June 29, 2010 at 02:32:40
Specs: Microsoft Windows XP Professional, 2.992 GHz / 3325 MB
Hi!

I have a text file (*.sos) containing lines like this

.PUNKT 938:
..PTEMA 4255
..Q_KM_FOLK SEL
..VEGG-KONST 5-4-6
..VEGG-KLED 2
..VEGG-FARGE 9-grå
..SYNLIGHET 1
.PUNKT 939:
..PTEMA 4255
..Q_KM_FOLK FRI
..VEGG-KONST 1-3
..VEGG-KLED 4
..VEGG-FARGE 8-gul
..SYNLIGHET 1

(… indicates that the actual file contains many more similar lines.)

I would like to have a bat-file that finds the strings ”..VEGG-KONST” and ”..VEGG-FARGE” in the input file (*.sos) and replaces them with one line for each ”parameter” (e.g. 5, 4 and 6 for the first occurrence of ”..VEGG-KONST”), so that the output file (with _1 added to the original filename; *_1.sos) looks like this


.PUNKT 938:
..PTEMA 4255
..Q_KM_FOLK SEL
..VEGG-KONST 5
..VEGG-KONST 4
..VEGG-KONST 6
..VEGG-KLED 2
..VEGG-FARGE 9
..VEGG-FARGE grå
..SYNLIGHET 1
.PUNKT 939:
..PTEMA 4255
..Q_KM_FOLK FRI
..VEGG-KONST 1
..VEGG-KONST 3
..VEGG-KLED 4
..VEGG-FARGE 8
..VEGG-FARGE gul
..SYNLIGHET 1

Is it possible to create a bat-file carrying out these two tasks (finding and replacing/adding)?

I would appreciate any help.
Thanks.
Best regards
Anne, Norway


See More: Replace and inserting new lines in text file

Report •


✔ Best Answer
June 29, 2010 at 15:27:25
I can't say for certain if this would work, but adding an additional line:

IF /I "%%a" EQU "" GOTO :Read_File

immediately before or after:

IF /I "%%a" EQU "..VEGG-KONST" CALL :Token_Parser %%b &GOTO :Read_File
IF /I "%%a" EQU "..VEGG-FARGE" CALL :Token_Parser %%b &GOTO :Read_File

may accomplish removal of blank lines, if that's what you desire.
My idea is untested, so as always, try on copies of files, or files
you don't care for.

Anyway, I'm glad I was able to help! :-)

Insomniac at large



#1
June 29, 2010 at 04:50:23
Hi Anne,
The following is setup to run against only one file to get your desired results:

::21807.bat
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET sos_path=\Your\Path\To\sos\File
SET sos_temp=sos%RANDOM%sos.sos
SET /A skip_count=0

:Read_File
FOR /F "%skip% tokens=1* delims= " %%a IN (%sos_path%\21807.sos) DO (
    SET /A skip_count+=1
    SET skip=skip=!skip_count!
    IF /I "%%a" EQU "..VEGG-KONST" CALL :Token_Parser %%b &GOTO :Read_File
    IF /I "%%a" EQU "..VEGG-FARGE" CALL :Token_Parser %%b &GOTO :Read_File
    >>%sos_path%\%sos_temp% ECHO.%%a %%b&GOTO :Read_File
)

MOVE /Y %sos_path%\%sos_temp% %sos_path%\21807.sos
EXIT /B

:Token_Parser
SET /A token_counter=1
:Token_Loop
FOR /F "tokens=%token_counter% delims=-" %%c IN ("%1") DO (
    >>%sos_path%\%sos_temp% ECHO %%a %%c
    SET /A token_counter+=1
    GOTO :Token_Loop
)
GOTO :EOF

If you need to do all .sos files, it could be changed to do a search for those
types of files. Let me know!

Insomniac at large


Report •

#2
June 29, 2010 at 05:36:56
Hi orangeboy!

Thanks a lot for your bat-codes. It works perfectly.
And yes, I would like to run it on a bunch of sos-files (*.sos) in a folder.

Thanks.
Best regards
Anne


Report •

#3
June 29, 2010 at 07:49:08
Here is a new version that looks for all *.sos files in a folder,
(and subfolders) and updates them accordingly:

::21807.bat
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET sos_path=\Your\Path\To\.sos\files
FOR /F "delims=" %%f IN ('DIR /B /S "%sos_path%\*.sos"') DO (
    CALL :File_Processor %%f
)
PAUSE
EXIT /B

:File_Processor
SET sos_temp=sos!RANDOM!sos.tmp
SET skip=
SET /A skip_count=0
ECHO.Working on %~1
:Read_File
FOR /F "%skip% usebackq tokens=1* delims= " %%a IN ("%~1") DO (
    SET /A skip_count+=1
    SET skip=skip=!skip_count!
    IF /I "%%a" EQU "..VEGG-KONST" CALL :Token_Parser %%b &GOTO :Read_File
    IF /I "%%a" EQU "..VEGG-FARGE" CALL :Token_Parser %%b &GOTO :Read_File
    >>"%sos_path%\!sos_temp!" ECHO.%%a %%b&GOTO :Read_File
)
MOVE /Y "%sos_path%\!sos_temp!" "%~1"
GOTO :EOF

:Token_Parser
SET /A token_counter=1
:Token_Loop
FOR /F "tokens=%token_counter% delims=-" %%c IN ("%1") DO (
    >>"%sos_path%\!sos_temp!" ECHO %%a %%c
    SET /A token_counter+=1
    GOTO :Token_Loop
)
GOTO :EOF

I personally don't think it's very pretty, but it seems to be functional :-)

Insomniac at large


Report •

Related Solutions

#4
June 29, 2010 at 14:37:15
Thanks again orangeboy!

Your code works fine.
However, the resulting files contain many blank/empty lines. I created a new bat-file running through "new" sos-files (the resulting files after running your script), deleting the blank lines and saving the new sos-files (without the blank lines) in a new folder (c:\Gammel_D\nye_sosifiler).

clean.bat (stored in the same folder as the "new" sos-files) looks like this:

REM Deleting blank lines
@ECHO OFF

FOR %%h in (*.sos) do (
ECHO.Working on %%h
For /F "eol= tokens=* delims=" %%A in (%%h) Do (
@echo %%A >> c:\Gammel_D\nye_sosifiler\%%h
)
)

(I did not manage to include the removing of blank lines within your code.)

Thanks for your help - saved me a lot of time.

Best regards
Anne


Report •

#5
June 29, 2010 at 15:27:25
✔ Best Answer
I can't say for certain if this would work, but adding an additional line:

IF /I "%%a" EQU "" GOTO :Read_File

immediately before or after:

IF /I "%%a" EQU "..VEGG-KONST" CALL :Token_Parser %%b &GOTO :Read_File
IF /I "%%a" EQU "..VEGG-FARGE" CALL :Token_Parser %%b &GOTO :Read_File

may accomplish removal of blank lines, if that's what you desire.
My idea is untested, so as always, try on copies of files, or files
you don't care for.

Anyway, I'm glad I was able to help! :-)

Insomniac at large


Report •

#6
June 30, 2010 at 01:28:12
What a beauty.
Just adding

IF /I "%%a" EQU "" GOTO :Read_File

immediately before

IF /I "%%a" EQU "..VEGG-KONST" CALL :Token_Parser %%b &GOTO :Read_File

did the trick (removed all blank lines).

Thank you once again for your time and effort.

Best regards
Anne


Report •

Ask Question