MLapaj
MLapaj

Reputation: 401

Batch scripting - parsing file line by line and finding string

I'm trying to parse a .txt file using batch script, line by line, untill I find "arg =" string and then get the following number. To put it into context, I'm trying to parse this gdb.txt file

warning: No executable has been specified and target does not support
determining executable automatically.  Try using the "file" command.
0x00007c2c in ?? ()
Loading section .sec1, size 0x20000 lma 0x0
Start address 0x8560, load size 131072
Transfer rate: 103 KB/sec, 1110 bytes/write.
Command Executed successfully: semihosting enable

Breakpoint 1 at 0x790a: file C:\LMA\ws_new\wam_sdk1886.31.001.1C_ver1\src\sdk\wam\bsp\detail/exit.c, line 21.
Note: automatically using hardware breakpoints for read-only addresses.

Breakpoint 1, exit (arg=0) at C:\LMA\ws_new\wam_sdk1886.31.001.1C_ver1\src\sdk\wam\bsp\detail/exit.c:21
21    volatile std::uint8_t a = 0;
arg = 0
[Inferior 1 (Remote target) detached]

I've come up with these few lines of batch script:

@echo off

for /f delims^=^ eol^= %%A in (gdb.txt) Do (
  echo %%A
  findstr /c:"arg =" %%A>nul 2>nul
  echo %errorlevel%
  )

I would like the script to recognize the line with "arg =" so I can read 0 afterwards. However this script seems not to be able to recognize "arg =" and always prints %errorlevel% as 1. What am I missing here?

Upvotes: 0

Views: 80

Answers (3)

Compo
Compo

Reputation: 38623

If I was doing this in a , I'd do it like this:

@For /F Tokens^=* %%G In ('%SystemRoot%\System32\findstr.exe /BIRC:"arg = [0123456789]" "gdb.txt" 2^> NUL') Do @Set /A %%G 2> NUL
@Set arg & Pause

The second line is there just to show you the results. You should replace that with your own code.

The first line could possibly be made shorter too, (although I wouldn't recommend it)!

@For /F Tokens^=* %%G In ('FindStr /BIRC:"arg = [0-9]" gdb.txt')Do @Set /A %%G

Upvotes: 1

Stephan
Stephan

Reputation: 56180

It's much easier to filter the file for the wanted line instead of searching through each line (and much faster, especially with big files):

for /f "tokens=2 delims== " %%A in ('type gdb.txt^|findstr /bic:"arg = "') Do set "var=%%A"
echo arg is %var%.

Note: should there be more than one matching line, this will give you the last result.

Upvotes: 1

Magoo
Magoo

Reputation: 80033

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q64803488.txt"
SET "arg="
FOR /f "usebackq tokens=1-3" %%a IN ("%filename1%") DO (
 IF /i "%%a"=="arg" IF "%%b"=="=" SET "arg=%%c"
)

ECHO arg found was "%arg%"

GOTO :EOF

You would need to change the setting of sourcedir to suit your circumstances. The listing uses a setting that suits my system.

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

The usebackq option is only required because I chose to add quotes around the source filename.

Set arg to empty to ensure it isn't already set.

For each line in the file, tokenise using the default delimiter set (which includes space) and select the first 3 tokens. If the first (%%a) is arg (/i to make case-insensitive) and the second in %%b is = then assign the third (%%c) to arg.

Upvotes: 1

Related Questions