Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 528
  • Last Modified:

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

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
sunhux
Asked:
sunhux
  • 4
  • 3
  • 2
  • +2
5 Solutions
 
Chris DentPowerShell DeveloperCommented:
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
 
Bill PrewCommented:
Grab a copy of unxutils from http://unxutils.sourceforge.net/ it has head, tail, and grep.

~bp
0
 
Bill PrewCommented:
Or you can grab them from http://gnuwin32.sourceforge.net/packages.html

~bp
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Daz_1234Commented:
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
 
sunhuxAuthor Commented:


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
 
Bill PrewCommented:
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
 
Steve KnightIT ConsultancyCommented:
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
 
Steve KnightIT ConsultancyCommented:
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
 
Steve KnightIT ConsultancyCommented:
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
 
sunhuxAuthor Commented:
excellent
0
 
Steve KnightIT ConsultancyCommented:
So did  you go with powersheel in the end then?
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now