Page 1 of 1
[SOLVED] Extract each whole line that contains a specific word.
Posted: 02 Mar 2020 10:58
by PAB
Good afternoon,
I have a
.wccf file that is a couple of thousand lines long.
I would like to
extract every complete line that contains the word
true please.
I have tried the below code but I just cant seem to get anything to work!
Code: Select all
for /F "tokens=*" %%A in ("MyFile.wccf") do [process] %%A
Code: Select all
for /F "true=; tokens=2,3* delims=," %%i in ("MyFile.wccf") do @echo %%i %%j %%k
Code: Select all
for /F "tokens=*" %%A in ('type "MyFile.wccf"') do [process] %%A
Code: Select all
@echo off
SETLOCAL DisableDelayedExpansion
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ MyFile.wccf"`) do (
set "var=%%a"
SETLOCAL EnableDelayedExpansion
set "var=!var:*:=!"
echo(!var!
ENDLOCAL
)
Thanks in advance.
Re: Extract each whole line that contains a specific word.
Posted: 02 Mar 2020 11:36
by aGerman
Something like that probably
and if you have to process these lines in a loop then
Code: Select all
for /f "delims=" %%i in ('findstr "\<true\>" "MyFile.wccf"') do echo %%i
Steffen
Re: Extract each whole line that contains a specific word.
Posted: 02 Mar 2020 12:08
by PAB
Thanks for the reply, it is appreciated.
Unfortunately, it is giving me a %%i was unexpected at this time. error even though I ran it from an elevated cmd prompt and used C:\Users\UserName\Desktop\MyFile.wccf!
Re: Extract each whole line that contains a specific word.
Posted: 02 Mar 2020 13:01
by elzooilogico
Code: Select all
for /f "delims=" %%i in ('findstr "\<true\>" "MyFile.wccf"') do echo %%i
if you are running it in a batch script
Code: Select all
for /f "delims=" %i in ('findstr "\<true\>" "MyFile.wccf"') do echo %i
if you run it in command line
Re: Extract each whole line that contains a specific word.
Posted: 02 Mar 2020 13:14
by PAB
Brilliant, thanks.
I don't know what I did before but it didn't work, whereas it seems to work now with . . .
Code: Select all
@echo off
for /f "delims=" %%i in ('findstr "\<true\>" "C:\Users\System-Admin\Desktop\MyFile.wccf"') do echo %%i >> "C:\Users\System-Admin\Desktop\wccfTrueOutput.txt"
Pause
Thanks everyone, it is appreciated!
Re: Extract each whole line that contains a specific word.
Posted: 02 Mar 2020 13:23
by aGerman
But in this case you definitely don't need the FOR loop. Just redirect the output of FINDSTR
Code: Select all
>"C:\Users\System-Admin\Desktop\wccfTrueOutput.txt" findstr "\<true\>" "C:\Users\System-Admin\Desktop\MyFile.wccf"
Steffen
Re: Extract each whole line that contains a specific word.
Posted: 02 Mar 2020 13:44
by PAB
aGerman wrote: ↑02 Mar 2020 13:23
But in this case you definitely don't need the FOR loop. Just redirect the output of FINDSTR
Code: Select all
>"C:\Users\System-Admin\Desktop\wccfTrueOutput.txt" findstr "\<true\>" "C:\Users\System-Admin\Desktop\MyFile.wccf"
Steffen
VERY nice, I like that, thank you Steffen, it is appreciated.
EDIT:
Sorry to be cheeky, but is there any way that I can get it to keep the
original file name [
C:\Users\System-Admin\Desktop\MyFile] so when it extracts the data it saves it as the original file name with a suffix of
.txt please?
Re: Extract each whole line that contains a specific word.
Posted: 02 Mar 2020 14:25
by aGerman
PAB wrote: ↑02 Mar 2020 13:44
Sorry to be cheeky, but is there any way that I can get it to keep the
original file name [
C:\Users\System-Admin\Desktop\MyFile] so when it extracts the data it saves it as the original file name with a suffix of
.txt please?
All we know so far from your code is that you write both the input and the output path straight into your script. The script doesn't update it's own code miraculously
If you pass the input path as parameter, or if you read it from a file, or if you receive the path from a pipeline, or ... then you should tell us and show the code you use since we don't own a crystal ball to see on your display.
Steffen
Re: Extract each whole line that contains a specific word.
Posted: 03 Mar 2020 09:04
by PAB
Sorry, I have just re-read my last post, and to be honest it is ridiculous on my part, I don't know what I was thinking, so please ignore it, it is my age.
What I am thinking of doing is to code the batch file to open a browse window so I can navigate to and select a file, and then have a box open up where I can enter the word to look for, and then save the file as the original file name but with a .txt suffix.
I will have a go at this later on in the week!