Grep needed (with look-behind assertion?)

34 views
Skip to first unread message

samar

unread,
Aug 13, 2021, 5:38:01 AM8/13/21
to bbe...@googlegroups.com
I'm struggling with a Grep Find/Replace action to apply to a file with sorted lines, each having three units separated by tabs:

first1    second11    third111
first1    second11    third112
first1    second11    third113
first1    second11    third114
first1    second11    third115
first2    second21    third211
first2    second22    third221
first2    second23    third231
first3    second31    third311
first3    second31    third312
first3    second31    third313
first3    second31    third314
first3    second32    third321
first4    second41    third411
first4    second41    third412
first4    second42    third421
first4    second42    third422
first5    second51    third511
first5    second52    third521
first5    second52    third522
first5    second52    third523
first5    second53    third531
...


Now I need to insert an underscore at the beginning of each line IF the "first" and "second" fields are identical to the respective fields in its previous line. The result should be this:

first1    second11    third111
_first1    second11    third112
_first1    second11    third113
_first1    second11    third114
_first1    second11    third115
first2    second21    third211
first2    second22    third221
first2    second23    third231
first3    second31    third311
_first3    second31    third312
_first3    second31    third313
_first3    second31    third314
first3    second32    third321
first4    second41    third411
_first4    second41    third412
first4    second42    third421
_first4    second42    third422
first5    second51    third511
first5    second52    third521
_first5    second52    third522
_first5    second52    third523
first5    second53    third531
...

I presume this could be achieved by using positive look-behind assertion, but all my attempts have failed so far.

Any help greatly is appreciated!

samar

Kaveh

unread,
Aug 13, 2021, 5:51:52 AM8/13/21
to bbe...@googlegroups.com
Hi Samar

From memory, positive look behind is limited, so use look ahead. This one works for me:

Search: (.+?\t.+?)\t(.+)\r(?=\1)
Replace: \1\t\2\r_\3

--
This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/0596413f-15bb-62f9-6cb7-47ad2bab4e08%40bluewin.ch.


--
Kaveh Bazargan PhD
Director
Accelerating the Communication of Research

samar

unread,
Aug 14, 2021, 1:56:49 AM8/14/21
to bbe...@googlegroups.com
This is exactly what I was looking for – it works perfectly. Many thanks, Kaveh!

Kaveh wrote on 13.08.21 11:51:

Kaveh

unread,
Aug 14, 2021, 6:33:30 PM8/14/21
to bbe...@googlegroups.com
Honor to have contributed to this list. :-)

--
This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
--
Sent from MetroMail
Reply all
Reply to author
Forward
0 new messages