How to print selective lines print in Batch program
Moderator: DosItHelp
How to print selective lines print in Batch program
i have a output like
abcd
bcdfeh
sdfd
not before
not after
kjgkj
fgjfgkjfl
lgjl;f
not before
not after
The output contain multiples of not before and not after. i want to print lines ( 3 lines) before and after of "Not before"
The belo
abcd
bcdfeh
sdfd
not before
not after
kjgkj
fgjfgkjfl
lgjl;f
not before
not after
The output contain multiples of not before and not after. i want to print lines ( 3 lines) before and after of "Not before"
The belo
Re: How to print selective lines print in Batch program
let me explain it again. The test not before and not after are not on the regular format....
The text is like this
qweere
erer
fefeer
ererer
erer
ererer
ererer
eerer
reere
not before
not after
dlfsdkfjasdkl
sklafjsdkl
sdfkljsdklf
sdklfjasdkl
not before
not after
fgdfgdfg
Here i want to print the lines like as follows i.e. a line pre and post search of the keyword(Not before).
reere
not before
not after
sdklfjasdkl
not before
not after
The text is like this
qweere
erer
fefeer
ererer
erer
ererer
ererer
eerer
reere
not before
not after
dlfsdkfjasdkl
sklafjsdkl
sdfkljsdklf
sdklfjasdkl
not before
not after
fgdfgdfg
Here i want to print the lines like as follows i.e. a line pre and post search of the keyword(Not before).
reere
not before
not after
sdklfjasdkl
not before
not after
Re: How to print selective lines print in Batch program
You posted in this thread - but you didn't follow up. viewtopic.php?p=20291#p20291
If you want help in this thread then please show an actual example of the text to be searched.
If you want help in this thread then please show an actual example of the text to be searched.
Re: How to print selective lines print in Batch program
ok, Here is the actual example. The output of the command will display the details of the certs of expiring in 2 yrs. else display only labels. i want to ignore the certs, if not having the expiry details
sample from original
! certificate bc
! cert asdd
! certificate a
Not before : April 28 2004
not after : april 28 2014
! certifi b
Not before : April 28 2004
not after : april 28 2014
- certifica c
Not before : April 28 2004
not after : april 28 2014
sample from original
! certificate bc
! cert asdd
! certificate a
Not before : April 28 2004
not after : april 28 2014
! certifi b
Not before : April 28 2004
not after : april 28 2014
- certifica c
Not before : April 28 2004
not after : april 28 2014
Re: How to print selective lines print in Batch program
Show us the text before and after. Include several examples.
Re: How to print selective lines print in Batch program
only the Certificate namesI1,2,3...) changed and rest of the content is same.
! certificate1
! certificate2
! certificate3
Not Before : April 28 2004
Not After : april 28 2014
! certificate4
! certificate5
Not Before : April 28 2004
Not After : april 28 2014
- certificcate6 (personnel certificate)
Not Before : April 28 2004
Not After : april 28 2014
! certificate1
! certificate2
! certificate3
Not Before : April 28 2004
Not After : april 28 2014
! certificate4
! certificate5
Not Before : April 28 2004
Not After : april 28 2014
- certificcate6 (personnel certificate)
Not Before : April 28 2004
Not After : april 28 2014
Re: How to print selective lines print in Batch program
Mahendra wrote:only the Certificate namesI1,2,3...) changed and rest of the content is same.
! certificate1
! certificate2
! certificate3
Not Before : April 28 2004
Not After : april 28 2014
! certificate4
! certificate5
Not Before : April 28 2004
Not After : april 28 2014
- certificcate6 (personnel certificate)
Not Before : April 28 2004
Not After : april 28 2014
And what do you want your output to look like from the example your provided?
Re: How to print selective lines print in Batch program
! certificate1
! certificate2
! certificate3
Not Before : April 28 2004
Not After : april 28 2014
! certificate4
! certificate5
Not Before : April 28 2004
Not After : april 28 2014
- certificcate6 (personnel certificate)
Not Before : April 28 2004
Not After : april 28 2014
The output required is:(* certificate name and their expiry details)
! certificate3
Not Before : April 28 2004
Not After : april 28 2014
! certificate5
Not Before : April 28 2004
Not After : april 28 2014
- certificcate6 (personnel certificate)
Not Before : April 28 2004
Not After : april 28 2014
! certificate2
! certificate3
Not Before : April 28 2004
Not After : april 28 2014
! certificate4
! certificate5
Not Before : April 28 2004
Not After : april 28 2014
- certificcate6 (personnel certificate)
Not Before : April 28 2004
Not After : april 28 2014
The output required is:(* certificate name and their expiry details)
! certificate3
Not Before : April 28 2004
Not After : april 28 2014
! certificate5
Not Before : April 28 2004
Not After : april 28 2014
- certificcate6 (personnel certificate)
Not Before : April 28 2004
Not After : april 28 2014
Re: How to print selective lines print in Batch program
Code: Select all
@echo off
setlocal EnableDelayedExpansion
findstr /N /C:"Not Before" input.txt > matchingLines.txt
call :ShowThreeLinesPerMatch < input.txt
del matchingLines.txt
goto :EOF
:ShowThreeLinesPerMatch
set lastLine=0
for /F "delims=:" %%a in (matchingLines.txt) do (
set /A skip=%%a-lastLine-1, lastLine=%%a+2
for /L %%i in (1,1,!skip!) do set /P line=
for /L %%i in (1,1,3) do set /P "line=!line!" & echo/
)
exit /B
Re: How to print selective lines print in Batch program
Nice lateral thinking. It can be simplified a bit.
Code: Select all
@echo off
setlocal EnableDelayedExpansion
findstr /N /C:"Not Before" input.txt > matchingLines.txt
set lastLine=0
for /F "delims=:" %%a in (matchingLines.txt) do (
set /A skip=%%a-lastLine-1, lastLine=%%a+2
for /L %%i in (1,1,!skip!) do set /P line=
for /L %%i in (1,1,3) do set /P "line=!line!" & echo/
)<input.txt
del matchingLines.txt
Re: How to print selective lines print in Batch program
I think I can beat that
How about a single FINDSTR call
You just need 3 search strings:
1) Any line that is 2 lines before a line that begins with "Not After"
2) Any line that is 1 line before a line that begins with "Not After"
3) Any line that starts with "Not After"
Or, simpler yet:
1) Any line that precedes a line that begins with "Not Before"
2) Any line that begins with "Not Before"
3) Any line that begins with "Not After"
Dave Benham

How about a single FINDSTR call


You just need 3 search strings:
1) Any line that is 2 lines before a line that begins with "Not After"
2) Any line that is 1 line before a line that begins with "Not After"
3) Any line that starts with "Not After"
Code: Select all
@echo off
setlocal enableDelayedExpansion
:: define LF as linefeed
set LF=^
:: 2 blank lines above are critical - DO NOT REMOVE
:: define CR as carriage return
for /f %%A in ('copy /Z "%~dpf0" nul') do set "CR=%%A"
findstr /r /c:"!LF!.*!CR!*!LF!Not After" /c:"!LF!Not After" /c:"^Not After" input.txt
Or, simpler yet:
1) Any line that precedes a line that begins with "Not Before"
2) Any line that begins with "Not Before"
3) Any line that begins with "Not After"
Code: Select all
@echo off
setlocal enableDelayedExpansion
:: define LF as linefeed
set LF=^
:: 2 blank lines above are critical - DO NOT REMOVE
findstr /r /c:"!LF!Not Before" /c:"^Not Before" /c:"^Not After" input.txt
Dave Benham
Re: How to print selective lines print in Batch program
That's even more lateral. I dips me lid to ye.
Re: How to print selective lines print in Batch program
Was reading this thread last night around 1:30am and I started to think of Dave's findstr trick. Was a little to tired and probably a little to drunk to try and fire up a computer last night.
Re: How to print selective lines print in Batch program
dbenham wrote:I think I can beat that![]()
How about a single FINDSTR call![]()
![]()
You just need 3 search strings:
1) Any line that is 2 lines before a line that begins with "Not After"
2) Any line that is 1 line before a line that begins with "Not After"
3) Any line that starts with "Not After"Code: Select all
@echo off
setlocal enableDelayedExpansion
:: define LF as linefeed
set LF=^
:: 2 blank lines above are critical - DO NOT REMOVE
:: define CR as carriage return
for /f %%A in ('copy /Z "%~dpf0" nul') do set "CR=%%A"
findstr /r /c:"!LF!.*!CR!*!LF!Not After" /c:"!LF!Not After" /c:"^Not After" input.txt
Dave Benham
Hey, Dave! This idea is very clever!

We could use the same method to write a general-purpose program that show groups of N lines of a file before a target line. Here it is:
Code: Select all
@echo off
setlocal enableDelayedExpansion
rem Usage: ShowNLinesEndingAtSTRInFileNAME.bat #oflines "search string" filename
:: define LF as linefeed
set LF=^
:: 2 blank lines above are critical - DO NOT REMOVE
:: define CR as carriage return
for /f %%A in ('copy /Z "%~dpf0" nul') do set "CR=%%A"
set regExpr=/R
rem Assemble regular expressions for lines separated more than one line from target line
set /A linesWithCR=%1-2
for /L %%i in (%linesWithCR%,-1,1) do (
set regExpr=!regExpr! /C:"
for /L %%j in (1,1,%%i) do (
set regExpr=!regExpr!@LF@.*@CR@*
)
set regExpr=!regExpr!@LF@.*%~2"
)
rem Assemble regular expression for the line before the target line
if %1 gtr 1 (
set regExpr=!regExpr! /C:"@LF@.*%~2"
)
rem Assemble regular expression for the target line
set regExpr=!regExpr! /C:"%~2"
setlocal DisableDelayedExpansion
set regExpr=%regExpr:@=!%
setlocal enableDelayedExpansion
findstr %regExpr% %3
Could be possible to get the lines after a target line in the same FINDSTR?

Antonio
Last edited by Aacini on 14 Dec 2012 07:30, edited 2 times in total.
Re: How to print selective lines print in Batch program
I like it
I don't think so. FINDSTR only prints out the first line when matching multiple lines, so I don't see how to print a trailing line when we don't know what it contains.
Dave Benham

Aacini wrote:Could be possible to get the lines after a target line in the same FINDSTR?![]()
I don't think so. FINDSTR only prints out the first line when matching multiple lines, so I don't see how to print a trailing line when we don't know what it contains.
Dave Benham