All, After having it pointed out that this will only work for the first instance of the string in the file by Dan Astoorian (Something I was already working on) and thinking it was a bit crap having to edit the script everytime you wanted to change the string or filename etc, I edited is as follows. It now does some fairly basic usage checking, and takes the parameters as command line arguments, and will print the output for all instances of the string. I called it 'greb' (Grep for Blocks) and stuck it in /usr/local/bin, so you can use it just like you would grep (Except don't trying piping output to it!) #!/usr/bin/ksh function usage { echo "Incorrect arguments" echo "Usage: greb -b nn -a nn string file" echo "Example: greb -b 10 -a 19 ERROR /tmp/logfile" } function main { NPRINT=`expr $BEFORE + $AFTER + 1` for LINENUM in `grep -n $STRING $FILE | awk -F: '{ print $1 }'` do LINESTRT=`expr $LINENUM - $BEFORE | sed 's/^ *//'` tail +$LINESTRT $FILE | head -$NPRINT echo "--" done } if [[ $1 = "-b" ]] && [[ $3 = "-a" ]] then BEFORE=$2 AFTER=$4 STRING=$5 FILE=$6 main else usage exit fi Regards dAn -----Original Message----- From: Wilkinson, Daniel [mailto:Daniel.Wilkinson@capgemini.co.uk] Sent: 18 June 2001 15:57 To: Sun Managers Mailing List Subject: SUMMARY: grep for more than single lines Thanks everyone - I shan't name you all, as I've has 20+ emails in 10 minutes already, and am aleady shamed by my pitifully woeful knowledge, without having to name those cleverer than I... The general opinion was to use GNU grep, but I wasn't keen on installing any more packages etc which is why I was wandering down the script route. If you can, or have Gnu grep installed, then do this: grep -A5 -B5 string file Another way was with sed: sed -n -e '/string/{=;x;1!p;g;$!N;p;D;}' -e h <inputfile to print 1 line before and after, although I didn't play around with it to do more than one line either way, because this weeks winner is Sarah Eckhause, with: #!/bin/sh BEFORE=10 AFTER=5 # Number of lines to print NPRINT=`expr $BEFORE + $AFTER + 1` # Find line number of the string in question LINENO=`grep -n 'string' FILETOSEARCH | awk -F':' '{ print $1 }' | head -1` LINESTRT=`expr $LINENO - $BEFORE | sed 's/^ *//'` tail +$LINESTRT FILETOSEARCH | head -$NPRINT Which does exactly what I want, in roundabout the way I was starting to try, although I had temporarily forgotten about head/tail, and was trying to trick more into doing something it'll never do... Thanks Sarah! Dan -----Original Message----- From: Wilkinson, Daniel [mailto:Daniel.Wilkinson@capgemini.co.uk] Sent: 18 June 2001 14:46 To: Sun Managers Mailing List Subject: grep for more than single lines All, I need a command or script to grep for a string within a file, and then instead of just printing out that single line, getting it to print the previous 10 lines, or the following 5 lines, or a combination of both, such that I can cut out an error message in context, and see what happened before or after etc. Am I being stupid, and does this exist as a command in Solaris, and if not, has anyone written anything already before I start? Regards dAn _______________________________________________ sunmanagers mailing list sunmanagers@sunmanagers.org http://www.sunmanagers.org/mailman/listinfo/sunmanagers _______________________________________________ sunmanagers mailing list sunmanagers@sunmanagers.org http://www.sunmanagers.org/mailman/listinfo/sunmanagersReceived on Mon Jun 18 17:09:02 2001
This archive was generated by hypermail 2.1.8 : Wed Mar 23 2016 - 16:24:57 EDT