Reputation: 79
set line = xxx/yyy/xxx/xxxxxxxx.h:32:#include "../../xxxx/xxxxx.h"
if this is the string, how can i extract just xxxxxxxx.h, between / and : in batch script?
i have included the bin path of cygwin to my windows environment variables so it can run grep commands. Current, this is my script:
@echo off
SETLOCAL enabledelayedexpansion
set /p search_parameter="Type your search: "
rem run grep seach commmand
grep -nri %search_parameter% --colour --include=*.{c,h} > text.txt
rem filter required lines into new text file
type text.txt | findstr /I "#include" | findstr /V "examples DELIVERY_REL" > text2.txt
rem read text file line-by-line
for /F "tokens=* delims=" %%G in (text2.txt) do (
set "line=%%G"
rem echo line: !line!
for /f "delims=:" %%i in ("!line!") do set "line=%%i"
for %%i in (!line:/= !) do set "line=%%i"
echo line: !line!
)
pause
echo on
Currently, this is my output:
line: line:/
line: line:/
line: line:/
line: line:/
line: line:/
line: line:/
line: line:/
line: line:/
line: line:/
line: line:/
The problem is this line:
for %%i in (!line:/= !) do set "line=%%i"
Upvotes: 1
Views: 940
Reputation: 79
@echo off
SETLOCAL enabledelayedexpansion
set /p search_parameter="Type your search: "
rem run grep seach commmand
grep -nri %search_parameter% --colour --include=*.{c,h} > text.txt
rem filter required lines into new text file
type text.txt | findstr /I "#include" | findstr /V "examples DELIVERY_REL" > text2.txt
rem read text file line-by-line
for /F "tokens=* delims=" %%G in (text2.txt) do (
set "line=%%G"
rem echo line: !line!
for /f "delims=:" %%i in ("!line!") do set "line=%%~nxi"
echo line: !line!
)
pause
echo on
Upvotes: 0
Reputation: 56180
Use a for /f
loop to get the part before the first :
.
Then use a plain for
to get the last token of this part (tokenize by replacing /
with spaces):
@echo off
setlocal
set "line=xxx/yyy/xxx/xxxxxxxx.h:32:#include "../../xxxx/xxxxx.h""
set line
for /f "delims=:" %%a in ("%line%") do set "line=%%a"
set line
for %%a in (%line:/= %) do set "line=%%a"
set line
Output of this code:
line=xxx/yyy/xxx/xxxxxxxx.h:32:#include "../../xxxx/xxxxx.h"
line=xxx/yyy/xxx/xxxxxxxx.h
line=xxxxxxxx.h
due to the exact format of your string, you can just:
set "line=xxx/yyy/xxx/xxxxxxxx.h:32:#include "../../xxxx/xxxxx.h""
for /f "delims=:" %%a in ("%line%") do set "line=%%~nxa"
(Thanks, @dbenham). This works because the parser "translates" the "wrong" slash (not valid for a filename) to the "correct" backslash and %%~nxa
just extracts the "filename and extension" from the "full file name". (for
treats the string as a filename but doesn't even care, if it's valid)
Edit
...
rem read text file line-by-line
for /F "tokens=* delims=" %%G in (text2.txt) do (
set "line=%%G"
for /f "delims=:" %%i in ("!line!") do set "line=%%~nxi"
echo line: !line!
)
Upvotes: 2