Page 1 of 1

Delete lines based upon string or file

Posted: 19 Jul 2018 13:18
by goodywp
Hi
I have a file which need to delete some lines based upon some strings
This file is called 829501.txt
  • \T501-08815-0103\Application_Signing\Signed_Schemes\QA_Infra-SL_0102_Signed_T3\

    .500006011000.S3S
    .500007011000.S3S
    .500008011000.S3S
    .500009011000.S3S
    .500010011000.S3S
    .500011011000.S3S
    .500012011000.S3S
    .500014011000.S3S
    .500015011800.S3S
    .500016011000.S3S
    .500028011201.S3S
    .500072010400.S3S
    .500129010200.S3S
    .500134010100.S3S
    .500142010100.S3S
    .500144010100.S3S
I have another file removal.txt
  • 50001101,
    50007201,
I have the below code to remove

Code: Select all

findstr /v "50001101 50007201" 829501.txt >updated.txt
after run, I got the correct result as below
  • \T501-08815-0103\Application_Signing\Signed_Schemes\QA_Infra-SL_0102_Signed_T3\

    .500006011000.S3S
    .500007011000.S3S
    .500008011000.S3S
    .500009011000.S3S
    .500010011000.S3S
    .500012011000.S3S
    .500014011000.S3S
    .500015011800.S3S
    .500016011000.S3S
    .500028011201.S3S
    .500129010200.S3S
    .500134010100.S3S
    .500142010100.S3S
    .500144010100.S3S
I am looking for two ways to improve the code
1) improve the code via using file removal.txt inside the code?

Code: Select all

findstr /v "50001101 50007201" 829501.txt >updated.txt 
2) or process file removal.txt as a one line string, in this case such as 50001101 50007201
so later I can use as a variable for the findstr command

Code: Select all

set sch=50001101 50007201
findstr /v "%sch%" 829501.txt >updated.txt


Maybe you have better solution than mine...

Thanks

Re: Delete lines based upon string or file

Posted: 19 Jul 2018 13:55
by pieh-ejdsch
if you use the file, without a concluding comma at the end of the line, you can also filter it with

Code: Select all

findstr /v /g:filterFile inputfile >outfile
Phil

Re: Delete lines based upon string or file

Posted: 19 Jul 2018 14:08
by goodywp
pieh-ejdsch wrote:
19 Jul 2018 13:55
if you use the file, without a concluding comma at the end of the line, you can also filter it with

Code: Select all

findstr /v /g:filterFile inputfile >outfile
Phil
Tried your code and exactly as you mentioned only work when the filterFile without a concluding comma at the end of the line...

Now my case is that this filterFile does have a concluding comma at the end of the line..

I need one more step to process the removal of the comma at the end of each line in this filterFile
looking for a way to do this
Thanks

Re: Delete lines based upon string or file

Posted: 19 Jul 2018 15:10
by goodywp
I tried another file as temp.txt

500011011000.S3S
500072010400.S3S

and run the following code not working, any reason?

Code: Select all

findstr /v /g:temp.txt 829501.txt >updated.txt
only the above removal.txt without comma working
50001101
50007201

Code: Select all

findstr /v /g:removal.txt 829501.txt >updated.txt

Re: Delete lines based upon string or file

Posted: 20 Jul 2018 06:50
by Squashman
goodywp wrote:
19 Jul 2018 15:10
I tried another file as temp.txt

500011011000.S3S
500072010400.S3S

and run the following code not working, any reason?

Code: Select all

findstr /v /g:temp.txt 829501.txt >updated.txt
only the above removal.txt without comma working
50001101
50007201

Code: Select all

findstr /v /g:removal.txt 829501.txt >updated.txt
Works just fine.

Code: Select all

C:\BatchFiles\Goodywp>type 829501.txt
.500006011000.S3S
.500007011000.S3S
.500008011000.S3S
.500009011000.S3S
.500010011000.S3S
.500011011000.S3S
.500012011000.S3S
.500014011000.S3S
.500015011800.S3S
.500016011000.S3S
.500028011201.S3S
.500072010400.S3S
.500129010200.S3S
.500134010100.S3S
.500142010100.S3S
.500144010100.S3S

C:\BatchFiles\Goodywp>type temp.txt
500011011000.S3S
500072010400.S3S

C:\BatchFiles\Goodywp>findstr /v /g:temp.txt 829501.txt
.500006011000.S3S
.500007011000.S3S
.500008011000.S3S
.500009011000.S3S
.500010011000.S3S
.500012011000.S3S
.500014011000.S3S
.500015011800.S3S
.500016011000.S3S
.500028011201.S3S
.500129010200.S3S
.500134010100.S3S
.500142010100.S3S
.500144010100.S3S

C:\BatchFiles\Goodywp>
And for the record we have shown you in your previous questions the usage of FINDSTR with the /G option. Not sure why that wasn't one of your first code examples.

Re: Delete lines based upon string or file

Posted: 20 Jul 2018 07:52
by goodywp
Squashman wrote:
20 Jul 2018 06:50
goodywp wrote:
19 Jul 2018 15:10
I tried another file as temp.txt

500011011000.S3S
500072010400.S3S

and run the following code not working, any reason?

Code: Select all

findstr /v /g:temp.txt 829501.txt >updated.txt
only the above removal.txt without comma working
50001101
50007201

Code: Select all

findstr /v /g:removal.txt 829501.txt >updated.txt
Works just fine.

Code: Select all

C:\BatchFiles\Goodywp>type 829501.txt
.500006011000.S3S
.500007011000.S3S
.500008011000.S3S
.500009011000.S3S
.500010011000.S3S
.500011011000.S3S
.500012011000.S3S
.500014011000.S3S
.500015011800.S3S
.500016011000.S3S
.500028011201.S3S
.500072010400.S3S
.500129010200.S3S
.500134010100.S3S
.500142010100.S3S
.500144010100.S3S

C:\BatchFiles\Goodywp>type temp.txt
500011011000.S3S
500072010400.S3S

C:\BatchFiles\Goodywp>findstr /v /g:temp.txt 829501.txt
.500006011000.S3S
.500007011000.S3S
.500008011000.S3S
.500009011000.S3S
.500010011000.S3S
.500012011000.S3S
.500014011000.S3S
.500015011800.S3S
.500016011000.S3S
.500028011201.S3S
.500129010200.S3S
.500134010100.S3S
.500142010100.S3S
.500144010100.S3S

C:\BatchFiles\Goodywp>
And for the record we have shown you in your previous questions the usage of FINDSTR with the /G option. Not sure why that wasn't one of your first code examples.
Thank you for your try out!!!
I rechecked the temp file and found the reason finally....
The temp.txt I have, had two spaces at the end of string, after removal. it back to work..... :oops:
BTW, is any easy way to make this temp.txt file without spaces at the end?? I tried to trace back the code and only found one place by removing one space. however, the result of this temp.txt still same end up two spaces at the end...

Tried out this one and works pretty well...

Code: Select all

if exist temp_rm.txt (del temp_rm.txt)

> temp_rm.txt  setLocal enableDELAYedeXpansioN
for /f "tokens=* delims= " %%a in (temp.txt) do (
call :sub1 %%a
>> temp_rm.txtecho.!S!
)

goto :eof

:sub1
set S=%*
goto :eof

Re: Delete lines based upon string or file

Posted: 20 Jul 2018 09:27
by pieh-ejdsch
used a combination with a for / f loop and a normal for loop. in the filter file can end semicolon, comma and space. in between, however, not - as well as no special characters like & () *? <> = | "

Code: Select all

>tempFilterFile (
  @ for /f "delims=" %%i in ("filterFile") do @ (
    for %%j in (%%i) do @ echo %%i
) )
findstr /v /g:tempFilterFile inFile >outFile
Or write the tempFilterFile simply use one for /f loop with
"delims= ,"

Phil

Re: Delete lines based upon string or file

Posted: 20 Jul 2018 09:50
by goodywp
pieh-ejdsch wrote:
20 Jul 2018 09:27
used a combination with a for / f loop and a normal for loop. in the filter file can end semicolon, comma and space. in between, however, not - as well as no special characters like & () *? <> = | "

Code: Select all

>tempFilterFile (
  @ for /f "delims=" %%i in ("filterFile") do @ (
    for %%j in (%%i) do @ echo %%i
) )
findstr /v /g:tempFilterFile inFile >outFile
Or write the tempFilterFile simply use one for /f loop with
"delims= ,"

Phil
Thanks after tried out your code and got the tempFilterFile do not have any list only have filterFile name in it..???

Re: Delete lines based upon string or file

Posted: 21 Jul 2018 01:03
by pieh-ejdsch
ups my mistake.
I forgot that filenames should be used in the for /f loop with the usebackQ option.
this damn cellphone.

Re: Delete lines based upon string or file

Posted: 23 Aug 2018 08:14
by goodywp
pieh-ejdsch wrote:
21 Jul 2018 01:03
ups my mistake.
I forgot that filenames should be used in the for /f loop with the usebackQ option.
this damn cellphone.
I tired the below code as you suggested, please correct me if I am wrong and still not working

Code: Select all

>tempFilterFile (
  @ for /f "usebackq tokens=*" %%i in ("filterFile") do @ (
    for %%j in (%%i) do @ echo %%i
) )
findstr /v /g:tempFilterFile inFile >outFile
I got the tempFilterFile which is still showing , at the end of each line. Therefore, it is still not removing the lines...

OK I got the results after modified the code as below:

Code: Select all

>TSA_debug_rm.txt (
  @ for /f "usebackq tokens=1* delims=," %%i in ("TSA_debug_remove.txt") do @ (
    for %%j in (%%i) do @ echo %%i
) )
findstr /v /g:TSA_debug_rm.txt Schemes_to_Sign_for_829501.txt >updateTSA.txt

Thanks a lot!