0

I try to grep like this:

grep -E '(UNIQ_23A0E66922726E9|UNIQ_23A0E66D943CFF3)' xst.sql

however I need some bytes (not lines) before and after the match.

if i use both -E and -P i got:

grep: conflicting matchers specified

I want functionality like

grep -o -P '.{0,10}UNIQ_23A0E66922726E9.{0,10}' xst.sql 

but for all strings at once - in the example I have 2 but in reality I grep for 1000+.

  • 2
    Please add some example in- and output to make it clear what you want. Also please clarity: your command prints the whole line with the match, you say you want something before/after the match. Do you mean "match", or "matching line"? – pLumo Oct 23 '19 at 7:28
  • This is not what the comment asked for. Data! – Panki Oct 23 '19 at 7:31
  • I posted update – Nick Oct 23 '19 at 7:33
  • This is not what I was asking for... I want input data and output data, not commands. Also, your new command will stop at line breaks. But a line break is also a byte. – pLumo Oct 23 '19 at 7:35
  • 1
    and are these 1000+ strings all of the same type? If so, this might work for you: grep -Po '.{0,10}(UNIQ_[0-9A-F]{15}).{0,10}' xst.sql – pLumo Oct 23 '19 at 7:39
2

-E tells GNU grep to use extended regular expressions (ERE), -P tells it to use Perl-compatible regular expressions (PCRE), which are a superset in functionality of ERE. Those are two different variants of the regex language, so indeed the options conflict with each other.

None of what you show needs PCRE though, so it seems you could just use

grep -o -E '.{0,10}(UNIQ_23A0E66922726E9|UNIQ_23A0E66D943CFF3).{0,10}' xst.sql

That would be a valid regex with the same meaning in PCRE, too. I'm not sure if there are performance differences between the ERE engine and the PCRE engine.

  • exactly what i wanted. I tried something like that but seems was different. works, will let you know when it matches something in 1-2 h – Nick Oct 23 '19 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.