Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
zagix
- Posts: 68
- Joined: 16 Oct 2013 23:19
#1
Post
by zagix » 18 Oct 2015 10:45
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.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#2
Post
by foxidrive » 19 Oct 2015 00:01
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
-
zagix
- Posts: 68
- Joined: 16 Oct 2013 23:19
#3
Post
by zagix » 19 Oct 2015 09:17
Hi,
Post deleted
Last edited by
zagix on 19 Oct 2015 14:43, edited 1 time in total.
-
Aacini
- Expert
- Posts: 1913
- Joined: 06 Dec 2011 22:15
- Location: México City, México
-
Contact:
#4
Post
by Aacini » 19 Oct 2015 10:21
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
-
Aacini
- Expert
- Posts: 1913
- Joined: 06 Dec 2011 22:15
- Location: México City, México
-
Contact:
#6
Post
by Aacini » 19 Oct 2015 16:04
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
-
penpen
- Expert
- Posts: 2009
- Joined: 23 Jun 2013 06:15
- Location: Germany
#7
Post
by penpen » 19 Oct 2015 16:36
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.
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#8
Post
by Squashman » 19 Oct 2015 16:44
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?
-
penpen
- Expert
- Posts: 2009
- Joined: 23 Jun 2013 06:15
- Location: Germany
#9
Post
by penpen » 19 Oct 2015 17:04
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 "(...)".
-
Yury
- Posts: 115
- Joined: 28 Dec 2013 07:54
#10
Post
by Yury » 19 Oct 2015 19:56
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.
-
zagix
- Posts: 68
- Joined: 16 Oct 2013 23:19
#11
Post
by zagix » 20 Oct 2015 01:40
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!!!
-
Yury
- Posts: 115
- Joined: 28 Dec 2013 07:54
#12
Post
by Yury » 20 Oct 2015 02:48
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
-
penpen
- Expert
- Posts: 2009
- Joined: 23 Jun 2013 06:15
- Location: Germany
#13
Post
by penpen » 20 Oct 2015 07:30
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
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#14
Post
by foxidrive » 20 Oct 2015 08:04
Thank you for making it easy for the contributors here.
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#15
Post
by Squashman » 06 Nov 2015 18:40
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.