Page 1 of 2

Search for String

Posted: 18 Oct 2015 10:45
by zagix
Hi,

Copy REPORT DATE :dd/mm/yyyy from the 3rd line situated in centre to the first line of text file.
BRANCH :abcdefg Branch space space space REPORT DATE :19/08/2015 space space space Report 1

Code: Select all

findstr /v "^To   Ln Code BRANCH      :" sample.txt >sample_revised.txt


1. I want to search a text file for occurrence of statements such as To__ Ln_Code but due to spaces in between To Ln Code it fails the search to delete.

2. _BRANCH :abcdefg Branch, Find and delete with case sensitive. Ignore space in start of _BRANCH and delete the row.

3. Delete spaces from start of character for | Branch Code : will become |Branch Code :

How to do this.

Re: Search for String

Posted: 19 Oct 2015 00:01
by foxidrive
Give actual data, and then another set of data showing what you want - and explain it using them.

Not meaning to be abrupt here, but
False data with "sort-of" explanations just wastes people's time, because the task inevitably changes.

See here: viewtopic.php?f=3&t=6108

Re: Search for String

Posted: 19 Oct 2015 09:17
by zagix
Hi,
Post deleted

Re: Search for String

Posted: 19 Oct 2015 10:21
by Aacini
Please, do the following: pretend you are one of us (that have NOT your data) and try to create the sample.txt file from the images you posted...

Now, post the data AS TEXT enclosed between "[code]" and "[/code]" tags! If the file is too large post just a section, but with REAL DATA!

Antonio

Re: Search for String

Posted: 19 Oct 2015 14:39
by zagix

Re: Search for String

Posted: 19 Oct 2015 16:04
by Aacini

Code: Select all

@echo off
setlocal EnableDelayedExpansion

for /F "skip=1 tokens=6 delims=: " %%a in (input.txt) do set "repDate=%%a" & goto break
:break

(
echo %repDate%
for /F "skip=5 delims=" %%a in (input.txt) do (
   set "line=%%a"
   if "!line:~0,16!" equ "|  Branch Code :" (
      echo ^|!line:~3,-1!        ^|
   ) else (
      echo %%a
   )
)
) > after.txt

Antonio

Re: Search for String

Posted: 19 Oct 2015 16:36
by penpen
Without the last tab:

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion
set "inputFile=sample.txt"
set "resultFile=result_%inputFile%"
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%inputFile%"') do set "lines=%%~a"
set "space= "

< "%inputFile%" (
> "%resultFile%" (
   for /L %%l in (1, 1, %lines%) do (
      set "line="
      set /P "line="
      if "!line:~0,14!" == "|  Branch Code" ( echo(!line:~0,1!!line:~3,-1!!line:~-2,1!!line:~-1!
      ) else if "!line:~0,1!" == "|" ( echo(!line!
      ) else if "%%~l" == "3" echo(!line:~86,10!!space!
   )
)
)
endlocal
goto :eof


penpen

Edit: Aacini was faster.

Re: Search for String

Posted: 19 Oct 2015 16:44
by Squashman
penpen wrote:Without the last tab:

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion
set "inputFile=sample.txt"
set "resultFile=result_%inputFile%"
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%inputFile%"') do set "lines=%%~a"
set "space= "

< "%inputFile%" (
> "%resultFile%" (
   for /L %%l in (1, 1, %lines%) do (
      set "line="
      set /P "line="
      if "!line:~0,14!" == "|  Branch Code" ( echo(!line:~0,1!!line:~3,-1!!line:~-2,1!!line:~-1!
      ) else if "!line:~0,1!" == "|" ( echo(!line!
      ) else if "%%~l" == "3" echo(!line:~86,10!!space!
   )
)
)
endlocal
goto :eof


penpen

Always wondered if you could stream in a file like that line by line. Is there any speed gain by doing it this way instead of just doing all the line manipulation in the FOR /F command?

Re: Search for String

Posted: 19 Oct 2015 17:04
by penpen
In most cases (i've seen) it should be faster.

But in the above case Aacinis verison should be much faster, because:
- he uses less variables, and
- prints the line with %%a.
So he avoids lookup variables within the environment block (slow).

I only created it (for fun) to match the "After.txt" file exactly - without the last tab (as i guess this is not wanted).

My code accesses the envrionment block 11 times per loop, and Aacinis only needs 3:
So his code should be ~3,66... times faster.
(I could optimize my code for example by using the exact charcters of the "Branch code"-line characters, instead of looking them up in the "line"-variable; but there are tabs and spaces mixed - and i guess you couldn't post that reliable, and i cannot reduce to 3 variable accesses anyway (if i see it right: 5 minimum, so his code should stay faster (1,66... times).)

penpen

Edit: Added the last block.
Edit2: Added "(...)".

Re: Search for String

Posted: 19 Oct 2015 19:56
by Yury

Code: Select all

@echo off
set "t=    "
<"sample.txt">"After.txt" (for /f "tokens=3 delims=:" %%i in ('cmd/v/c "(for /l %%i in (1 1 3) do set/p x=)& echo !x!"') do for /f %%j in ("%%i") do (echo %%j )
for /f "delims=" %%i in ('find "|"') do for /f "tokens=1,2 delims=|:" %%j in ("%%i") do if "%%j" equ "  Branch Code " (echo ^|Branch Code :%%k%t%^|) else (echo %%i))
exit/b


Replace four spaces in the second line of my code to the tab character, and the output file will be identical to the appropriate file from the Google Drive.

Re: Search for String

Posted: 20 Oct 2015 01:40
by zagix
Hi,

Thank you very much for your help Aacini,Penpen,Yury and Squashman for helping me.

One more query if i want to delete this lines also.

Code: Select all

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|Sr. No    |   FirstName   |   LastName   |   Company            |Address         |City      |County      |State   |ZIP   |Phone      |Fax      |Email         |Web                  |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|


Thank you VERRRRRRRRRRRRRRY MUCH!!! 8)

Re: Search for String

Posted: 20 Oct 2015 02:48
by Yury

Code: Select all

@echo off
set "t=    "
<"sample.txt">"After.txt" (for /f "tokens=3 delims=:" %%i in ('cmd/v/c "(for /l %%i in (1 1 3) do set/p x=)& echo !x!"') do for /f %%j in ("%%i") do (echo %%j )
for /f "skip=3 delims=" %%i in ('find "|"') do for /f "tokens=1,2 delims=|:" %%j in ("%%i") do if "%%j" equ "  Branch Code " (echo ^|Branch Code :%%k%t%^|) else (echo %%i))
exit/b

Re: Search for String

Posted: 20 Oct 2015 07:30
by penpen

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion
set "inputFile=sample.txt"
set "resultFile=result_%inputFile%"
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%inputFile%"') do set "lines=%%~a"
set "space= "

< "%inputFile%" (
> "%resultFile%" (
   for /L %%l in (1, 1, %lines%) do (
      set "line="
      set /P "line="
      if "%%~l" == "3" ( echo(!line:~86,10!!space!
      ) else if %%~l lss 10 ( rem:
      ) else if "!line:~0,14!" == "|  Branch Code" ( echo(!line:~0,1!!line:~3,-1!!line:~-2,1!!line:~-1!
      ) else if "!line:~0,1!" == "|" ( echo(!line!
      )
   )
)
)
endlocal
goto :eof


penpen

Re: Search for String

Posted: 20 Oct 2015 08:04
by foxidrive
zagix wrote:Hi,

File link: https://drive.google.com/file/d/0B3

After.
File link: https://drive.google.com/file/d/0B3o

Thanks.


Thank you for making it easy for the contributors here.

Re: Search for String

Posted: 06 Nov 2015 18:40
by Squashman
If my aging memory serves me correctly I remember Dave doing some testing on this streaming trick. He was specifically testing what was the fastest way to count the number of lines in the file. I can't remember if the FIND or FINDSTR command was faster. I suppose I could test when I get back to work. I always have large files to test with at work.