开发者

How to find a pattern and surrounding content in a very large SINGLE line file?

开发者 https://www.devze.com 2023-04-10 23:48 出处:网络
I have a very large file 100Mb+ where all the content is on one line. I wish to find a pattern in that 开发者_JAVA技巧file and a number of characters around that pattern.

I have a very large file 100Mb+ where all the content is on one line. I wish to find a pattern in that 开发者_JAVA技巧file and a number of characters around that pattern.

For example I would like to call a command like the one below but where -A and -B are number of bytes not lines:

cat very_large_file | grep -A 100 -B 100 somepattern

So for a file containing content like this:

1234567890abcdefghijklmnopqrstuvwxyz

With a pattern of

890abc
and a before size of -B 3 
and an after size of -A 3

I want it to return:

567890abcdef

Any tips would be great. Many thanks.


You could try the -o option:

-o, --only-matching
      Show only the part of a matching line that matches PATTERN.

and use a regular expression to match your pattern and the 3 preceding/following characters i.e.

grep -o -P ".{3}pattern.{3}" very_large_file 

In the example you gave, it would be

echo "1234567890abcdefghijklmnopqrstuvwxyz" > tmp.txt
grep -o -P ".{3}890abc.{3}" tmp.txt


Another one with sed (you may need it on systems where GNU grep is not available):

sed -n '
  s/.*\(...890abc...\).*/\1/p
  ' infile


Best way I can think of doing this is with a tiny Perl script.

#!/usr/bin/perl
$pattern = $ARGV[0];
$before = $ARGV[1];
$after = $ARGV[2];

while(<>) {
  print $& if( /.{$before}$pattern.{$after}/ );
}

You would then execute it thusly:

cat very_large_file | ./myPerlScript.pl 890abc 3 3

EDIT: Dang, Paolo's solution is much easier. Oh well, viva la Perl!

0

精彩评论

暂无评论...
验证码 换一张
取 消