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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looking for patterns in arrays

Status
Not open for further replies.

kayman

Technical User
Oct 8, 2003
2
GB
Hi there,

With the following script I can look for a specific pattern in the focus position of an array, called 'size' (this array contains a focus position with left and right context)

BEGIN{
if(right==0)
right=left
size=left+right+1
focus=left+1
}
{
print "linenumber= " NR
print "line= " $0
for(i=1; i<size; i++)
w=w[i+1]
w=$0

if(w[focus]~pattern){
for(i=1; i<=size; i++)
print i, w, w[i+1]
printf(&quot;\n&quot;)
}
}

Command line: gawk -f script.awk - v right=3 -v left=2 pattern=eeff filename.txt

PROBLEM: If the pattern occurs in the last n lines of the file (n=right), the array (with this pattern in the focus position) is not printed. What should I write in the END-block in order to fix this problem?

Please help me out here. Many thanks.

Kay

ps: I've created a test-file that looks like this:

aabb
ccdd
eeff
gghh
iijj
kkll
mmnn
oopp
qqrr
sstt
uuvv
eeff
yyzz



 
With your test-file, what is the result you should print ? Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top