korn shell - seaching a file for string, displaying lines around it
 
*
Welcome, Guest. Please login or register. November 22, 2008, 05:18:17 AM


Login with username, password and session length


Pages: [1]   Go Down
  Print  
Author Topic: korn shell - seaching a file for string, displaying lines around it  (Read 960 times)
0 Members and 1 Guest are viewing this topic.
ValentineSmith
Full Member
***
Posts: 29


« Reply #4 on: August 26, 2008, 11:54:57 PM »

Fair enough.
Thank you.
Logged
John R Peck
Administrator
Senior Member
*****
Posts: 52


« Reply #3 on: August 26, 2008, 11:45:25 PM »

integer START=LINE-0$1 END=1+0$1+0$2

- the zeroes are not needed because of the usage test,
- before I put the test in I put the zeroes in to ensure the
calculation doesn't fail if the parameters are not specified and
expand as null - just like quoting strings that might be unset.
« Last Edit: August 26, 2008, 11:47:17 PM by John R Peck » Logged
ValentineSmith
Full Member
***
Posts: 29


« Reply #2 on: August 26, 2008, 06:27:54 PM »

Very clever.
May I ask the purpose of those leading zeroes inside the loop body?
Logged
John R Peck
Administrator
Senior Member
*****
Posts: 52


« Reply #1 on: August 05, 2008, 11:51:54 AM »

That needs a shell script, here's a Korn shell version:


#!/usr/bin/ksh                                                           
#                                                                         
# Program to grep search FILE for a pattern MATCH and report X lines before and Y after.
#                                                                         
##########################################################################
                                                                         
if [[ -z "$1" || -z "$2" || -z "$3" || -z "$4" ]]                         
then                                                                     
  print -u2 "Usage: $0 X Y MATCH FILE"                                   
  print -u2 "X - lines before"                                           
  print -u2 "Y - lines after"                                             
  print -u2 "MATCH - grep search pattern"                                 
  print -u2 "FILE - file to search"                                       
else                                                                     
  grep -n $3 $4 | cut -d: -f1 | while read LINE                           
  do                                                                     
    integer START=LINE-0$1 END=1+0$1+0$2                                 
    tail +$START $4 | head -$END                                         
  done                                                                   
fi                                                                       


Logged
brent.weaver
Jr. Member
**
Posts: 13


« on: July 29, 2008, 09:53:45 PM »

Hello all. I am looking for a way to grep/search for a string in a file and show x lines before the located string, and y lines after.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC

Valid XHTML 1.0! Valid CSS! Dilber MC Theme by HarzeM
Page created in 0.622 seconds with 19 queries.




eXTReMe Tracker

Terms of Use and Privacy and Security Policies
Copyright 2001-2008 Michael Felt and ROOTVG.NET