• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

Gawk - Find all occurences of a string within XML - Also include 100 bytes before and after the match

On Windows, I am currently using gawk to find the first occurrence of a string + 100 bytes for all XMLs withing a directory:

gawk "/[some string]/" { match ( $0, /[some string]/); print substr($0,RSTART,RLENGTH + 100) FILENAME; }" C:\XML*.xml > C:\Results.txt

Open in new window


What I would like to do now is output all the matches (not just the first) to C:\Results.txt for each XML and also include 100 characters before the match + 100 characters after the match.

Is it possible to easily change this to get the desired results?

I understand that gawk might not be the best tool for the job, but this is just a one time task and if this is slow I can let this run overnight.
0
Mr P
Asked:
Mr P
1 Solution
 
ozoCommented:
If the 100 characters are on the same line as the match, you can use
match ( $0, /some string/){print substr($0,RSTART-100,RLENGTH + 200)FILENAME; }

if there can me more than one match on a line, and the matches are at least 100 characters apart, you might use
/some string/{while(match ( $0, /some string/)){ print substr($0,RSTART-100,RLENGTH + 200) FILENAME; $0=substr($0,RSTART+1)} }'
0
 
Mr PAuthor Commented:
This worked great.  Thank you, Ozo.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now