Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rewriting grep to capture matching record then to print the 5 lines 2

Status
Not open for further replies.

flyacr

Programmer
Feb 17, 2001
1
GB
I am trying to rewrite the grep command to capture the record(line) that has a pattern match and then to print out the matching line plus the 5 lines above and below the matched line so one can view the match in context. My problem is capturing the matched record. Here is a sample of what I have been trying. All HELP will be greatly appreciated.
#!/bin/bash
while getopts in OPTION
do
case "$OPTION" in
i) u_l_case=true ;;
n) number=true ;;
*) other=true ;;
esac
done
while [ $# -gt 0 ]
do
if echo $1 | grep -v '^-' > /dev/null 2>&1; then
if [ ! -a $1 ]; then
pattern=$1
echo "$pattern"
else
echo $1
fi
fi
shift
done
awk 'BEGIN {RS = "\n"}
{for (i=1; i<=NR; i++) {
record=$0
if (match(record, pattern)) {
printf (&quot;%s&quot;, record)}
}
}' $1 $pattern
 

Hi, flyacr!

I have semisolution for your problem. Maybe this awk script helps you. This script prints pattern with 3 rows before (you can make 5) and 1 row after:


patternFound == 1 { # print one more row and separator

print &quot; &quot; $0
print &quot;--&quot;

patternFound = 0
}


/pattern/ { # <== PUT HERE PATTERN

patternFound = 1

# print 3 rows before
for (i = 1; i <= 3; i ++)
print &quot; &quot; rowBefore

# print matched row
print &quot;>&quot; $0
}


# 3 rows before
{ rowBefore[1] = rowBefore[2]
rowBefore[2] = rowBefore[3]
rowBefore[3] = $0
}


I hope this helps you to get better idea.

Vers from Bible for you:
&quot;Behold, I stand at the door, and knock: if any man hear my voice, and open the door, I will come in to him, and will sup with him, and he with me,&quot; Jesus (Revelation 3:19).

KP.
 
Have you tried the [tt]-p[/tt] option of the [tt]grep[/tt] command?

I hope it works...
 
flyacr-


#!/bin/sh

# Usage: thisfile input pattern

pattern=$2



nawk 'BEGIN { FS = &quot;&quot; }

{
while ( $0 !~ /'&quot;$pattern&quot;'/ ) { # slurp records until pattern found
record[++i] = $0
getline

if ( $0 ~ /'&quot;$pattern&quot;'/ ) {

record[++i] = $0 # slurp pattern record
numrec = NR
getline

for ( j=1; j <= 5; j++ ) { # slurp 5 more records
record[++i] = $0
getline
}

record[++i] = $0

for ( k = numrec - 5 ; k <= numrec -1 ; k++ ) print record[k]
print &quot;&quot;
print record[numrec]
print &quot;&quot;

for ( x = numrec + 1 ; x <= numrec + 5 ; x++ ) print record[x]
print &quot;&quot;
print &quot;&quot;
next

for ( all in record) delete record[all]
}
}

}' $1 | more


# This is not fully tested, but seems to work.

Hope this helps!


flogrr
flogr@yahoo.com

 
#!/usr/bin/sh


# What is(are) the line number(s).
lines=`grep -n pattern file |awk -F &quot;:&quot; '{print $1}'`
echo $lines &quot;\n\n\n&quot;

for x in $lines
do
awk '{ if( NR >= &quot;'$x'&quot; - 5 && NR <= &quot;'$x'&quot; + 5) print NR, $0 }' file
echo &quot;\n\n&quot;
done

Succes Gregor Gregor.Weertman@mailcity.com
 
What is the point? I think you can use simply [tt]grep -p &quot;pattern&quot; files[/tt] to seek for a text and get the whole paragraph!

I hope it works...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top