How to print selective lines print in Batch program

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Mahendra
Posts: 26
Joined: 23 Sep 2012 02:29

How to print selective lines print in Batch program

#1 Post by Mahendra » 24 Sep 2012 05:18

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

Mahendra
Posts: 26
Joined: 23 Sep 2012 02:29

Re: How to print selective lines print in Batch program

#2 Post by Mahendra » 24 Sep 2012 07:21

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to print selective lines print in Batch program

#3 Post by foxidrive » 24 Sep 2012 08:02

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.

Mahendra
Posts: 26
Joined: 23 Sep 2012 02:29

Re: How to print selective lines print in Batch program

#4 Post by Mahendra » 24 Sep 2012 08:15

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to print selective lines print in Batch program

#5 Post by foxidrive » 24 Sep 2012 08:36

Show us the text before and after. Include several examples.

Mahendra
Posts: 26
Joined: 23 Sep 2012 02:29

Re: How to print selective lines print in Batch program

#6 Post by Mahendra » 24 Sep 2012 11:33

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

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How to print selective lines print in Batch program

#7 Post by Squashman » 24 Sep 2012 11:45

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?

Mahendra
Posts: 26
Joined: 23 Sep 2012 02:29

Re: How to print selective lines print in Batch program

#8 Post by Mahendra » 24 Sep 2012 11:48

! 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

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to print selective lines print in Batch program

#9 Post by Aacini » 17 Nov 2012 21:00

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to print selective lines print in Batch program

#10 Post by foxidrive » 17 Nov 2012 22:44

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


dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to print selective lines print in Batch program

#11 Post by dbenham » 18 Nov 2012 06:55

I think I can beat that :wink:

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to print selective lines print in Batch program

#12 Post by foxidrive » 18 Nov 2012 07:11

That's even more lateral. I dips me lid to ye.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How to print selective lines print in Batch program

#13 Post by Squashman » 18 Nov 2012 09:36

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.

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to print selective lines print in Batch program

#14 Post by Aacini » 19 Nov 2012 11:00

dbenham wrote:I think I can beat that :wink:

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! :D

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? :shock:

Antonio
Last edited by Aacini on 14 Dec 2012 07:30, edited 2 times in total.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to print selective lines print in Batch program

#15 Post by dbenham » 19 Nov 2012 12:48

I like it :)

Aacini wrote:Could be possible to get the lines after a target line in the same FINDSTR? :shock:

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

Post Reply