Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-09-10
11
Medium Priority
?
520 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 71

Accepted Solution

by:
Chris Dent earned 400 total points
ID: 36518638
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 57

Assisted Solution

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

~bp
0
 
LVL 57

Expert Comment

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

~bp
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 13

Expert Comment

by:Daz_1234
ID: 36522022
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
ID: 36523490


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
 
LVL 57

Assisted Solution

by:Bill Prew
Bill Prew earned 800 total points
ID: 36523540
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
ID: 36526262
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 800 total points
ID: 36526321
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 800 total points
ID: 36526327
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
ID: 36816048
excellent
0
 
LVL 43

Expert Comment

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

Featured Post

TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

688 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