parsing file and shortening selected lines

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Roe5685
Posts: 9
Joined: 17 May 2012 20:37

parsing file and shortening selected lines

#1 Post by Roe5685 » 22 Aug 2014 09:00

i use Dostip's BatchSubstitute.bat to change text in selected lines ( it searches for the old text ) of a file.
that works fine.
what i want to do now is instead of substituting a new expression for the old text is to eliminate the old text and shorten that line of the file down to the first 25 letters.
Unfortunately I do not understand enough of how BatchListOfFiles.bat works to do that.
Help would be appreciated.

The text of BatchSubstitute.bat:

@echo off
REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION

::BatchSubstitude - parses a File line by line and replaces a substring"
::syntax: BatchSubstitude.bat OldStr NewStr File
:: OldStr [in] - string to be replaced
:: NewStr [in] - string to replace with
:: File [in] - file to be parsed
:$changed 20100115
:$source http://www.dostips.com
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
set "line=%%B"
if defined line (
call set "line=echo.%%line:%~1=%~2%%"
for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
) ELSE echo.
)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: parsing file and shortening selected lines

#2 Post by foxidrive » 22 Aug 2014 09:15

Code: Select all

@echo off
>file.txt echo abc old text def12345678901234567890
type file.txt | repl "old text" "" L | repl "^(.{1,25}).*" "$1" >newfile.txt


This uses a helper batch file called `repl.bat` (by dbenham) - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.

Roe5685
Posts: 9
Joined: 17 May 2012 20:37

Re: parsing file and shortening selected lines

#3 Post by Roe5685 » 22 Aug 2014 10:28

Thank you for your prompt reply. I tried it but it did NOT remove search word ( STOP PRESS removes search word if you do not put " around it ) and it shortened all the lines! ( instead of only the lines containing the search word. )

Here is my revised input:

rem >file.txt echo abc old text def12345678901234567890 rem I did not understand this line. without the REM still not work properly.

type harry.eee | repl Hindi American L | repl "^(.{1,100}).*" "$1" > newharry.fff rem swaps Hindi to American fine but shortens all the lines in the file.

rem It worked fine at 25 but i changed it to extend past the word "Hindi" to see if your code eliminated it. It did if Hindi without quotes. The replacement to American without quotes worked too.

Open issues Only shorten the Hindi lines and suppose you wanted a phrase of a few words. Apparently it not work with quotes around it?

And thanks for the one line code to do a word swap or shorten lines ! That's impressive. Yes I looked at the very long Repl file!
A few sample commands to extend my use of this one liner would be appreciated.

Thanks again. Roe5685.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: parsing file and shortening selected lines

#4 Post by foxidrive » 22 Aug 2014 10:57

Roe5685 wrote:Thank you for your prompt reply. I tried it but it did NOT remove search word


Yes it does. Try it as it is written.

and it shortened all the lines! ( instead of only the lines containing the search word. )


Yep, that's my mistake.

Roe5685
Posts: 9
Joined: 17 May 2012 20:37

Re: parsing file and shortening selected lines

#5 Post by Roe5685 » 22 Aug 2014 12:27

ok thanks i await your correction.
and the search word without the quotes works but suppose the search word is a phrase like "harry is home"
Thanks,
Roe5685

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: parsing file and shortening selected lines

#6 Post by foxidrive » 23 Aug 2014 00:50

The search term is old text and it works.

Post Reply