[SOLVED] Extract each whole line that contains a specific word.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
PAB
Posts: 139
Joined: 12 Aug 2019 13:57

[SOLVED] Extract each whole line that contains a specific word.

#1 Post by PAB » 02 Mar 2020 10:58

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.
Last edited by PAB on 22 May 2020 09:26, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Extract each whole line that contains a specific word.

#2 Post by aGerman » 02 Mar 2020 11:36

Something like that probably

Code: Select all

findstr "\<true\>" "MyFile.wccf"
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

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Extract each whole line that contains a specific word.

#3 Post by PAB » 02 Mar 2020 12:08

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!

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Extract each whole line that contains a specific word.

#4 Post by elzooilogico » 02 Mar 2020 13:01

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

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Extract each whole line that contains a specific word.

#5 Post by PAB » 02 Mar 2020 13:14

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!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Extract each whole line that contains a specific word.

#6 Post by aGerman » 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

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Extract each whole line that contains a specific word.

#7 Post by PAB » 02 Mar 2020 13:44

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?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Extract each whole line that contains a specific word.

#8 Post by aGerman » 02 Mar 2020 14:25

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 :lol:
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

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Extract each whole line that contains a specific word.

#9 Post by PAB » 03 Mar 2020 09:04

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!

Post Reply