2

My grep command is it:

grep -Pzo -a 'Start(.*\n)*?.*?End' testfile.txt

And testfile.txt contains:

ItsTestStartFromHereEndNotVisibleStartFrom
HereEndOkNotVisible

the output:

$ grep -Pzo -a 'Start(.*\n)*?.*?End' testfile.txt

StartFromHereEndStartFrom
HereEnd

It works fine, but when null character exists between "Start" and "End", it does not work. I know it's because I have used "-z" option, but I need it for multi-line support.

For example, it's my content with null character:

ItsTestStartFrom[\x00]HereEndNotVisibleStart[\x00]From
HereEndOkNotVisible
alihardan
  • 183
  • 1
  • 14

1 Answers1

2

You can use perl instead

$ cat -A ip.txt
ItsTestStartFrom^@HereEndNotVisibleStart^@From$
HereEndOkNotVisible$

$ # -0777 will slurp the entire file, so NUL won't create issues
$ perl -0777 -ne 'print /Start.*?End/sg' ip.txt | cat -A
StartFrom^@HereEndStart^@From$
HereEnd$ 

$ perl -0777 -nE 'say /Start.*?End/sg' ip.txt
StartFromHereEndStartFrom
HereEnd

In given OP's sample, there is no single record matching because the NUL character occurs between Start and End sections...

$ cat -A ip.txt 
ItsTestStartFrom^@HereEndNotVisibleStart^@From$
HereEndOkNotVisible$
Start 3243$
asdf End asd$
$ grep -Pzo '(?s)Start.*?End' ip.txt
Start 3243
asdf End$ 
Sundeep
  • 23,246
  • 2
  • 28
  • 103
  • Thanks, It's worked. but my file size is big(More than two gigs), and when used your perl command, my RAM became full and my pc becam very slowly. but when I was using "grep", it's was very fast without high memory usage. is any solution exists? Thanks. – alihardan Jan 03 '18 at 08:41
  • grep would use NUL as record separator, hence why it didn't fill up memory and was fast... but perl would slurp entire file... I'll add alternate solution if possible.. – Sundeep Jan 03 '18 at 10:06