help needed .. search and copy required data from text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
massij
Posts: 5
Joined: 24 Mar 2014 02:30

help needed .. search and copy required data from text file

#1 Post by massij » 24 Mar 2014 02:34

I need desperate help in creating a batch file that reads from a log/txt file, searches for a specific word ex Computer name and then writes to another text file the words/digits below before.
I don't think I managed to make myself clear..the log file would contain say " asgf agahi a hi af as L124334 Computer name" I require L124334

I am new to batch programming and would appreciate any help :D

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

Re: help needed .. search and copy required data from text f

#2 Post by foxidrive » 24 Mar 2014 02:47

See how this works for you:

This uses a helper batch file called `repl.bat` - 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.

Code: Select all

type file.log | repl ".* (.*?) computer .*" "$1" ai >newfile.txt 

massij
Posts: 5
Joined: 24 Mar 2014 02:30

Re: help needed .. search and copy required data from text f

#3 Post by massij » 24 Mar 2014 03:06

I could not really understand what the file does :S

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

Re: help needed .. search and copy required data from text f

#4 Post by foxidrive » 24 Mar 2014 03:36

massij wrote:I could not really understand what the file does :S


Tell me what happened when you ran it?

I added a command to write the result into newfile.txt

massij
Posts: 5
Joined: 24 Mar 2014 02:30

Re: help needed .. search and copy required data from text f

#5 Post by massij » 24 Mar 2014 03:57

Thanks mate :D I managed to use it. Is there anychance that you could maybe explain to me how you managed to do this or the concepts behind it ? so that I could maybe learn to implement it for myself ? thanks mate :)

massij
Posts: 5
Joined: 24 Mar 2014 02:30

Re: help needed .. search and copy required data from text f

#6 Post by massij » 24 Mar 2014 04:32

for example how could I alter the file to show me the name if it follows and not preceeds the "Computer Name "

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

Re: help needed .. search and copy required data from text f

#7 Post by foxidrive » 24 Mar 2014 05:07

massij wrote:for example how could I alter the file to show me the name if it follows and not preceeds the "Computer Name "


This will work for the word following `computer` if there are at least two words following `computer`.
The term is a regular expression - to do it in plain batch then the makeup of the text file is a factor and needs to be known.

Code: Select all

type file.log | repl ".* computer (.*?) .*" "$1" ai >newfile.txt

massij
Posts: 5
Joined: 24 Mar 2014 02:30

Re: help needed .. search and copy required data from text f

#8 Post by massij » 24 Mar 2014 05:31

OK thanks... and there is no way that I could somehow manage to write the code myself without using the other batch file ? since I would like to understand the code and be able to build on it

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

Re: help needed .. search and copy required data from text f

#9 Post by foxidrive » 24 Mar 2014 05:49

massij wrote:OK thanks... and there is no way that I could somehow manage to write the code myself without using the other batch file ? since I would like to understand the code and be able to build on it


You can learn regular expressions, which you will find useful in the future,
or you can get a plain batch solution but it will not be as robust as repl.bat and you will have to show the makeup of the file because batch code parsing relies on the file contents.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: help needed .. search and copy required data from text f

#10 Post by penpen » 24 Mar 2014 06:07

So this may help you, but it easily could disturbed for example by " and ! characters:

Code: Select all

@echo off
setlocal enableDelayedExpansion
for /F "tokens=* delims=" %%a in ('findstr /I "Computer name" "log.txt"') do (
   set "line= %%a"
   for /F "tokens=* delims=" %%b in (" Computer name!line:*Computer name=!") do set "praefix=!line:%%b=!"
   for %%b in (!praefix!) do set "token=%%b"
   echo(!token!
)
enbdlocal
goto :eof

log.txt wrote:asdf
asgf agahi a hi af as L124334 Computer name
asgf agahi a hi af as !L124334! Computer name
asgf agahi a hi af as L124334! Computer name
asgf agahi a hi af as !L124334 Computer name
asgf agahi a hi af "as L124334" Computer name
asdf
asdf
In addition fixidrive's method should be faster.

penpen

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: help needed .. search and copy required data from text f

#11 Post by penpen » 24 Mar 2014 06:25

If you want to support ! characters this may help you:

Code: Select all

@echo off
setlocal disableDelayedExpansion
setlocal enableDelayedExpansion

for /F "tokens=* delims=" %%a in ('findstr /I "Computer name" "log.txt"') do (
   endlocal
   set "line= %%a"
   setlocal enableDelayedExpansion

   for /F "tokens=* delims=" %%b in (" Computer name!line:*Computer name=!") do (
      endlocal
      call set "praefix=%%line:%%b=%%"
      setlocal enableDelayedExpansion
   )

   for %%b in (!praefix!) do (
      endlocal
      set "token=%%b"
      setlocal enableDelayedExpansion
   )
   echo(!token!
)
endlocal
endlocal
goto :eof
(Even more slow.)

penpen

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: help needed .. search and copy required data from text f

#12 Post by Compo » 24 Mar 2014 08:26

penpen wrote:If you want to support ! characters this may help you:

Code: Select all

<Snip>
</Snip>
(Even more slow.)

penpen

Similarly

Code: Select all

@Echo Off&SetLocal
Set "_="&Set "_T="
For /f "Tokens=*" %%A In ('Find /i "Computer Name"^<"file.log"') Do Call :SUB1 %%A$
Ping -n 6 127.0.0.1 1>Nul
GoTo :Eof

:SUB1
(Set _=%*)
(Set _T=Computer Name%_:*Computer Name=%)
Call :SUB2 %%_:%_T%=%%
Echo( %_T%
GoTo :Eof

:SUB2
For %%A In (%*) Do Set "_T=%%A"

Post Reply