Solved

Windows script/freeware of  tail & Find/N with m lines above & n lines below displayed

Posted on 2011-09-10
11
436 Views
Last Modified: 2012-06-27
Currently we do at DOS prompt,
  FIND /N/I "string" [[drive:][path]filename
but I would like a freeware/script that can also display m number of lines above
& n number of lines below the returned found string with the line number
(ie /N & /I ignore-case feature) being displayed too.  


Also, would like a Unix-like tail freeware/script (to do "tail -5f file")
to continuously display 'newly refreshed' last 5 lines of the file.
Wintail & Baretail doesn't quite work (some pop-ups appear).
0
Comment
Question by:sunhux
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 70

Accepted Solution

by:
Chris Dent earned 100 total points
Comment Utility
Select-String in PowerShell can give you before and after context on an Windows system. For example, this command shows one line of context before and after each match:
Select-String -Pattern APatternToFind -Path YourFile.txt -Context 1

Open in new window

While PowerShell has a lot of other text processing functionality, it doesn't have a native replacement for tail (well, not an efficient one anyway). However, someone else has written one and bundled it in here (I'm not sure if it will do the 5 lines you want or not):

http://pscx.codeplex.com/

See "Get-Help Get-FileTail".

It's not DOS, but it is free :) If that doesn't work, I don't really have a decent option for you short of loading up Cygwin.

Chris
0
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 200 total points
Comment Utility
Grab a copy of unxutils from http://unxutils.sourceforge.net/ it has head, tail, and grep.

~bp
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Or you can grab them from http://gnuwin32.sourceforge.net/packages.html

~bp
0
 
LVL 12

Expert Comment

by:Daz_1234
Comment Utility
I have a copy of Tail.exe from the Windows 2003 resource kit.

It does not seem to work as:
tail -5f test.log
... or:
tail -5 -f test.log

... but does work as:
tail -f test.log   <auto updates, starts at last 10>
tail -20 test.log  <show last 20, not auto update>

Remove the .txt from the end of the attachment to use.

Hope this helps,
Daz.
tail.exe.txt
0
 

Author Comment

by:sunhux
Comment Utility


Thanks chaps.

I'll still need (as the grep from sourceforge unixutls doesn't meet this requirement):

 FIND /N/I "string" [[drive:][path]filename
but I would like a freeware/script that can also display m number of lines above
& n number of lines below the returned found string with the line number
(ie /N & /I ignore-case feature) being displayed too.  
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 200 total points
Comment Utility
The GNU port should support that as in:

Context control:
  -B, --before-context=NUM  print NUM lines of leading context
  -A, --after-context=NUM   print NUM lines of trailing context

http://gnuwin32.sourceforge.net/packages/grep.htm

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
Hmm, now of course it would be perfectly possible to write a VBScript to buffer the last 4-5 lines of a file and then output those when it comes across a match, even ib batch... except handling of special characters and blank lines would be an issue.  in VBScript it could be something like the attached code.  You can soon amend the output formatting or the arguments required, at the moment it is along the lines of:

C:\Utils\scripts>cscript //nologo findlines.vbs JAMIE_10232010_211145_Software.txt "184" 1
-----------------------
[211]  Windows 2000 Hotfix - KB905414   Ver: 20050816.13004
*[212]  Windows 2000 Hotfix - KB905495  Ver: 20050805.184113
[211]  Windows 2000 Hotfix - KB905495   Ver: 20050805.184113
-----------------------
-----------------------
[231]  Windows 2000 Hotfix - KB917953   Ver: 20060425.201939    Installed: 15/06/2006
*[232]  Windows 2000 Hotfix - KB918118  Ver: 20061117.184220    Installed: 17/02/2007
[231]  Windows 2000 Hotfix - KB918118   Ver: 20061117.184220    Installed: 17/02/2007
-----------------------
-----------------------
[278]  Windows 2000 Hotfix - KB944533   Ver: 20071210.120000    Installed: 17/02/2008
*[279]  Windows 2000 Hotfix - KB945553  Ver: 20080215.184910    Installed: 11/04/2008
[278]  Windows 2000 Hotfix - KB945553   Ver: 20080215.184910    Installed: 11/04/2008
-----------------------


i.e. line numbers are shown in [] and the line with string in is shown with * and a line of --- before and after each find.  Number of lines to show is part of the commandline as is filename and string to find.  
REM Script to look for a string in a text file and show a number of lines either side of each found string
REM Usage: cscript //nologo findit.vbs "filename.txt" "string to find" x
REM "x" is the number of lines to show before and after each find
REM By default shows a line of "----" before and after each find.
REM Allows maximum of 20 line buffer as it stands
REM Stephen Knight.  http://scripts.dragon-it.co.uk/

dim objFSO, objFile
dim args,strFind,strFileName,intLines,LineNum, ShowNext
dim buffer(20)

set args = wscript.Arguments
if args.count <2 then
  wscript.echo "There were not enough command line entries given.  Usage: cscript //nologo findit.vbs filename.txt string-to-find 5"
  wscript.quit
end if

strFileName=args(0)
strFind=lcase(args(1))
if args.count=2 then
  intLines=5
else
  intLines=args(2)
  if intLines > 20 then intLines=20
end if

if intLines="" then intLines
LineNum=1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName)

Do Until objFile.AtEndOfStream
  AddToBuffer objFile.ReadLine
  LineNum=LineNum+1
loop
objFile.Close

'Move buffer(intLines) down to buffer(num-1) etc. and add new line to buffer(5)
Sub AddToBuffer(nextline)
  for n=intLines to 1 step -1
    buffer(n-1)=buffer(n)
  next
  buffer(intLines)=nextline
  if instr(lcase(nextline),strFind) >0 then
    ShowBuffer
  end if

  if ShowNext>0 then
    wscript.echo "[" & linenum-intlines+n & "]  " & nextline
    ShowNext=ShowNext-1
    if ShowNext=0 then wscript.echo "-----------------------"
  end if
  
End Sub

' Echo buffer to screen.  Show next "intLines" lines too
Sub ShowBuffer
  wscript.echo "-----------------------"
  for n=0 to intlines-1
    wscript.echo "[" & linenum-intlines+n & "]  " & buffer(n)
  next
  ShowNext=intLines
  wscript.echo "*[" & linenum & "]  " & buffer(intLines)

End Sub

Open in new window

0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 200 total points
Comment Utility
Hmm pasted an old version there, got the "showbuffer" line number code copy/pasted wrong.
REM Script to look for a string in a text file and show a number of lines either side of each found string
REM Usage: cscript //nologo findit.vbs "filename.txt" "string to find" x
REM "x" is the number of lines to show before and after each find
REM By default shows a line of "----" before and after each find.
REM Allows maximum of 20 line buffer as it stands
REM Stephen Knight.  http://scripts.dragon-it.co.uk/

dim objFSO, objFile
dim args,strFind,strFileName,intLines,LineNum, ShowNext
dim buffer(20)

set args = wscript.Arguments
if args.count <2 then
  wscript.echo "There were not enough command line entries given.  Usage: cscript //nologo findit.vbs filename.txt string-to-find 5"
  wscript.quit
end if

strFileName=args(0)
strFind=lcase(args(1))
if args.count=2 then
  intLines=5
else
  intLines=args(2)
  if intLines > 20 then intLines=20
end if

if intLines="" then intLines
LineNum=1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName)

Do Until objFile.AtEndOfStream
  AddToBuffer objFile.ReadLine
  LineNum=LineNum+1
loop
objFile.Close

'Move buffer(intLines) down to buffer(num-1) etc. and add new line to buffer(5)
Sub AddToBuffer(nextline)
  if ShowNext>0 then
    wscript.echo "[" & linenum & "]  " & nextline
    ShowNext=ShowNext-1
    if ShowNext=0 then wscript.echo "-----------------------"
  end if
  
  for n=intLines to 1 step -1
    buffer(n-1)=buffer(n)
  next
  buffer(intLines)=nextline
  if instr(lcase(nextline),strFind) >0 then
    ShowBuffer
  end if
  
End Sub

' Echo buffer to screen.  Show next "intLines" lines too
Sub ShowBuffer
  wscript.echo "-----------------------"
  for n=0 to intlines-1
    wscript.echo "[" & linenum-intlines+n & "]  " & buffer(n)
  next
  ShowNext=intLines
  wscript.echo "*[" & linenum & "]  " & buffer(intLines)

End Sub

Open in new window

0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 200 total points
Comment Utility
Which gives you output like:
-----------------------
[103]  Microsoft Windows 2000 Resource Kit      Ver: 5.0.2092.1 Installed: 26/01/2010
[104]  Microsoft Windows 2000 Resource Kit      Ver: 5.0.2092.1 Installed: 26/01/2010
[105]  Microsoft Windows 2000 Resource Kit      Ver: 5.0.2092.1 Installed: 26/01/2010
[106]  Microsoft Windows 2000 Resource Kit      Ver: 5.0.2092.1 Installed: 26/01/2010
[107]  Microsoft Windows 2000 Resource Kit      Ver: 5.0.2092.1 Installed: 26/01/2010
*[108]  Microsoft Windows Journal Viewer        Ver: 1.5.2316.3 Installed: 13/10/2004
[109]  Microsoft XML Parser and SDK     Ver: 4.10.9406.0        Installed: 05/08/2005
-----------------------
[104]  Microsoft Windows Journal Viewer Ver: 1.5.2316.3 Installed: 13/10/2004
[105]  Microsoft Windows Journal Viewer Ver: 1.5.2316.3 Installed: 13/10/2004
[106]  Microsoft Windows Journal Viewer Ver: 1.5.2316.3 Installed: 13/10/2004
[107]  Microsoft Windows Journal Viewer Ver: 1.5.2316.3 Installed: 13/10/2004
[108]  Microsoft Windows Journal Viewer Ver: 1.5.2316.3 Installed: 13/10/2004
*[109]  Microsoft XML Parser and SDK    Ver: 4.10.9406.0        Installed: 05/08/2005
[110]  Mozilla Firefox (3.0.19) Ver: 3.0.19 (en-GB)
[111]  MSXML 4.0 SP2 (KB927978) Ver: 4.20.9841.0        Installed: 18/11/2006
[112]  MSXML 4.0 SP2 (KB936181) Ver: 4.20.9848.0        Installed: 15/08/2007
[113]  MSXML 4.0 SP2 (KB954430) Ver: 4.20.9870.0        Installed: 13/11/2008
[114]  MVision  Ver: 10.51.2027 Installed: 18/12/2009
-----------------------

C:\Utils\scripts>cscript //nologo findlines.vbs JAMIE_10232010_211145_Software.txt "184" 1
-----------------------
[211]  Windows 2000 Hotfix - KB905414   Ver: 20050816.13004
*[212]  Windows 2000 Hotfix - KB905495  Ver: 20050805.184113
[213]  Windows 2000 Hotfix - KB905749   Ver: 20050902.21643
-----------------------
-----------------------
[231]  Windows 2000 Hotfix - KB917953   Ver: 20060425.201939    Installed: 15/06/2006
*[232]  Windows 2000 Hotfix - KB918118  Ver: 20061117.184220    Installed: 17/02/2007
[233]  Windows 2000 Hotfix - KB918899   Ver: 20060725.123917    Installed: 13/08/2006
-----------------------
-----------------------
[278]  Windows 2000 Hotfix - KB944533   Ver: 20071210.120000    Installed: 17/02/2008
*[279]  Windows 2000 Hotfix - KB945553  Ver: 20080215.184910    Installed: 11/04/2008
[280]  Windows 2000 Hotfix - KB947864   Ver: 20080215.120000    Installed: 11/04/2008
-----------------------
-----------------------
[330]  Windows 2000 Hotfix - KB972260   Ver: 20090722.120000    Installed: 30/07/2009
*[331]  Windows 2000 Hotfix - KB972270  Ver: 20091015.141840    Installed: 15/01/2010
[332]  Windows 2000 Hotfix - KB973346   Ver: 20090707.111747    Installed: 15/07/2009
-----------------------
-----------------------
[350]  Windows 2000 Hotfix - KB978601   Ver: 20100215.181733    Installed: 14/04/2010
*[351]  Windows 2000 Hotfix - KB978706  Ver: 20091228.184506    Installed: 10/02/2010
[352]  Windows 2000 Hotfix - KB979309   Ver: 20100113.191139    Installed: 14/04/2010
-----------------------

Open in new window

0
 

Author Closing Comment

by:sunhux
Comment Utility
excellent
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
So did  you go with powersheel in the end then?
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now