-1

I try to match a a group twice in multiline.

grep -Pz 'Source: (?<group>.*/).*\n.*\n.*\n.*\k<group>|$'

it work except one major problem - it doesn't, \k<group> search for .*/ and not for the same characters that the first occurrence of group matched, which is normal considering that backreferencing is dynamic.

Is it possible to get «static» backreference, meaning that if (?<test>\d\d) match 45 , the next call to group test will always look for 45?

I'm not really good at explaining technical terms in english so feel free to yell at me or simply tell me to be more specific.

(I use perl grep here but if you have a solution who doesn't use it, go ahead)

Edit:

To answer the comment below: 1: I have a script that bulk move a lot of images files depending of ex if data. It works flawlessly but I want to test if in some cases the source Dir is the same as the output Dir.

A sample of a move: [1/1] (TEST - no files are being moved/copied)
Source: /disk/media/Camera/IMG_20130611_183722.jpg
Date/Time: 2013-06-11 16:37:23
Corresponding Tags: Composite:GPSDateTime
Destination (move): ./2013/06-Jun/20130611_163723.IMG_20130611_183722.jpg

Here the input and output are not the same so I want the script to not detect anything but if

Source: /disk/media/Camera/IMG_20130611_183722.jpg

Goes to

Destination (move): /disk/media/Camera/20130611_163723.IMG_20130611_183722.jpg

I want to highlight the four lines. The purpose is not to retrieve the data for anything else but just to visually identify quickly the detected files.

With the regexp up there it always highlight since the second occurrence of the group is dynamic (for grep it doesn't matter that the both are not identical)

  • Can you please (1) explain (in the question, not as a comment!) better what you are trying to accomplish, and post (2) a sample input, where the above command does something close to that you want, (3) the output you want, and (4) the output you're getting? – Scott Mar 27 '15 at 19:53
  • When I run your command on your data, I get the entire file. What are you getting? – Scott Mar 27 '15 at 20:36
  • Yes that the purpose of the |$ at the end. All the file are displayed but only the matched part are highlighted – eephyne Mar 27 '15 at 20:37
  • Oh, you want the entire input file displayed, but you have grep aliased to grep --color, and you want the groups of four lines that match your regex to be highlighted in color! It sure would have been nice if you’d mentioned that in your question! – Scott Mar 27 '15 at 20:59
1

OK, I think I’ve got it.  Your Source: (?<group>.*/).*\n regex is capturing, in the group group, everything after the Source:  up through the last / on the line.  So, for your example, it is capturing /disk/media/Camera/.  To capture the JPEG image filename, you want

Source: .*/(?<group>.*)\n…

OK, here we go again.  I believe that you are getting the static backreference that you want.  The problem is that you aren’t getting the backreference that you think you’re getting.  I think your capture group (and subsequent backreference) is just /.  I don’t understand why that would happen, except to make a wild guess that the .* is operating in non-greedy mode, so .*/ captures everything up through the first /.  Maybe the ? is somehow being interpreted to mean that.

I guess what you want might be

Source: (?<group>.*/)[^/]*\n…

which forces the .*/ to capture everything up through the last /.

  • Like I said in the question this is the folder that I want not the filename so my group is fine. My question is about "static" backreference. – eephyne Mar 27 '15 at 21:44
  • What's wrong with this? – Scott Mar 27 '15 at 22:46
  • @Scott I agree with your second analysis - it seems that (?<group>.*/) is capturing non-greedily in this context. – steeldriver Mar 27 '15 at 23:27
  • @steeldriver: That sounds like a bug to me (or, alternatively, bad documentation). What do you think? – Scott Mar 27 '15 at 23:29
  • @Scott it certainly appears inconsistent to me, but I'm not competent enough with PCRE to know whether that's the intended behavior or not – steeldriver Mar 28 '15 at 0:15

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.