Reputation: 648
I have text file which contains the follwing
Pool p c Dev Dev Total Enabled Used Free a b
------------ - - ----- ------------ -------- -------- -------- -------- --- ---
FC100 T F FBA RAID-3(1+1) 13849.1 13849.1 13119.4 7292.0 0 Ena
SATA500 T S FBA RAID-3(1+1) 50019.2 50019.2 46974.5 3044.9 Ena 0
I want to display extract FC100 and SATA500 from the file because those two lines contain "Ena". I have very little batch script experience so with my limited knowledge I came up with following script.
@ECHO OFF
setlocal enabledelayedexpansion
FOR /F "tokens=1,* delims= " %%a in (list.txt) DO (
if "%%b" NEQ "" (
set string=%%b
set substring= Ena
echo !string! |findstr "!substring!" > nul
if errorlevel 1 (
rem echo !SubString!
) else (
echo %%a
)
)
)
What is happening with the above code is that I am getting the required output but I am also getting Pool because the line contains Ena in Enabled. How do extract lines which only contain Ena and not match with Enabled.
Current Output
Pool
FC100
SATA500
I tried to use some regex magic with findstr but its not working out for me. Note - I can solve this problem in Perl but unfortunately I cannot install Perl on the system so I have to do this in batch.
Upvotes: 2
Views: 364
Reputation: 56238
replace
echo !string! |findstr "!substring!" > nul
with
echo !string! |findstr "!substring!" |findstr /v "Enabled" > nul
or even simpler:
echo !string! |findstr "Ena" |findstr /v "Enabled" > nul
first findstr
will include all lines with "Ena", second findstr /v
will exclude all lines with "Enabled".
Upvotes: 0
Reputation: 67326
@echo off
for /F "skip=1" %%a in ('findstr /C:" Ena" list.txt') do (
echo %%a
)
EDIT: New solution that seek for lines precisely with "Ena" string in any part
@echo off
for /F %%a in ('findstr /R /C:"\<Ena\>" list.txt') do (
echo %%a
)
Upvotes: 3
Reputation: 77737
You probably want to match Ena
as a whole word. To do that, you can surround your search term with \<
and \>
, i.e. change the substring
assignment like this:
set "substring=\<Ena\>"
And by the way, there's probably no need to assign substring
to the same value at every iteration of the loop. You can assign it just once before the loop.
Upvotes: 2
Reputation: 2824
Try this:
set substring="Ena[^a-zA-Z]"
[^a-zA-Z]
instructs findstr
not to use strings which contain letters after Ena
.
Upvotes: 0