Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

batch script - read line by line

Tags:

batch-file

I have a log file which I need to read in, line by line and pipe the line to a next loop.

Firstly I grep the logfile for the "main" word (like "error") in a separate file - to keep it small. Now I need to take the seperate file and read it in line by line - each line needs to go to another loop (in these loop I grep the logs and divide it in blocks) but I stuck here.

The log looks like

xx.xx.xx.xx - - "http://www.blub.com/something/id=?searchword-yes-no" 200 - "something_else" 

with a for /f loop I just get the IP instead of the complete line.

How can I pipe/write/buffer the whole line? (doesn't matter what is written per line)

like image 783
pille Avatar asked Dec 24 '10 18:12

pille


People also ask

How can I read the last 2 lines of a file in batch script?

set /A i-=1 ) set "lastLine[1]=%%a" ) for /L %%i in (%2,-1,1) do if defined lastLine[%%i] echo ! lastLine[%%i]! The version below is more efficient: @echo off setlocal EnableDelayedExpansion rem Tail command in pure Batch, version 2: Tail.

How do you read a batch script?

Reading of files in a Batch Script is done via using the FOR loop command to go through each line which is defined in the file that needs to be read. Since there is a no direct command to read text from a file into a variable, the 'for' loop needs to be used to serve this purpose.

What is %% A in batch script?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.


1 Answers

Try this:

@echo off for /f "tokens=*" %%a in (input.txt) do (   echo line=%%a ) pause 

because of the tokens=* everything is captured into %a

edit: to reply to your comment, you would have to do that this way:

@echo off for /f "tokens=*" %%a in (input.txt) do call :processline %%a  pause goto :eof  :processline echo line=%*  goto :eof  :eof 

Because of the spaces, you can't use %1, because that would only contain the part until the first space. And because the line contains quotes, you can also not use :processline "%%a" in combination with %~1. So you need to use %* which gets %1 %2 %3 ..., so the whole line.

like image 175
wimh Avatar answered Oct 14 '22 16:10

wimh