Zoso619
Zoso619

Reputation: 177

Using space as delimiter and print the text on CMD prompt in Windows batch

I have the below piece of line placed in a note pad. I want to use “space ” as the delimiter and print the word in list basis using Windows batch programming . I have already written the code for reading the note pad and displaying the output on the command prompt. How I will write the code for using space as a delimiter and display the output in a list on command prompt.

For ex.

[Sun Mar 7 23:30:23 2004] [info] [client 64.242.88.10] (104)Connection reset by peer: client stopped connection before send body completed

This is my context in notepad. My intent is to use space as delimiter starting from (104)connection to the end of the line and display it as list.

So the output on the command console should be like:

(104)Connection
Reset
By
peer:
client
stopped
connection
before
send
body
completed

Upvotes: 0

Views: 3175

Answers (2)

Aacini
Aacini

Reputation: 67226

This solution starts when you have the required line in a text file called theLine.txt. I am sorry, but I don't understand what you mean with "I have the line placed in a note pad". You may generate this file by just redirecting your output from the "command prompt" (that is, the screen) into the file. For example:

find "%search_string%" C:\log%_store%.txt > theLine.txt

This is the solution:

@echo off
setlocal


rem Create here "theLine.txt" input file with the required line


rem Read the line from the input file
set /P "line=" < theLine.txt

rem Separate the line in words at spaces and show they
set "wordFound="
for %%a in ("%line: =" "%") do (
   if %%a equ "(104)Connection" set "wordFound=true"
   if defined wordFound echo %%~a
)

This is the output shown in the command console by this solution:

(104)Connection
reset
by
peer:
client
stopped
connection
before
send
body
completed

Upvotes: 1

Magoo
Magoo

Reputation: 80113

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q35150865.txt"
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO CALL :process %%a

GOTO :EOF

:process
SET "prefix="
goTO loope

:loop
SHIFT
:loope
SET "word=%~1"
IF NOT DEFINED word GOTO :eof
:ops
IF "%prefix%"=="f" ECHO(%~1&GOTO loop
IF "%word:~0,1%"=="[" SET "prefix=s"
IF "%word:~-1%"=="]" SET "prefix=e"&GOTO loop
IF "%prefix%"=="s" GOTO loop
IF "%prefix%"=="e" SET "prefix=f"&GOTO ops
GOTO loop

You would need to change the setting of sourcedir to suit your circumstances.

I used a file named q35150865.txt containing your data for my testing.

This process simply applies each line from the source file to a subroutine, so the line appears to be a series of parameters.

The subroutine processes the line; first initialising prefix acts as a flag to specify whether or not we are in the prefix seof a line (ie up to the last ])

If the word found starts [ then prefix is set to s (start of unwanted line-prefix)

If the word found ends ] then prefix is set to e (potential end of unwanted line-prefix) and the next word is processed.

If we still have s in prefix then we need to continue until we get e so get next word.

If we have a word that neither starts nor ends with a bracket and we've got to the end of the prefix, then set prefix to f (we've finished with the prefix.) and we need to output the current word.


Here's another approach:

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q35150865.txt"
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO CALL :process1 %%a

GOTO :EOF

:process1
SET "word=%*"
:process1a
SET "short=%word:*]=%"
IF "%short%" neq "%word%" SET "word=%short%"&GOTO process1a
CALL :process2 %short%
GOTO :eof

:process2
SET "word=%~1"
IF DEFINED word ECHO(%~1&shift&GOTO process2
GOTO :eof

Upvotes: 0

Related Questions