GREP pattern to replace the first element with a tab

2021-03-20 Thread samar
Hi This is the first time a post a message here. I'm looking for something which I assume is pretty easy to accomplish with GREP but I fail to see how. I have a large text file with entries sorted in this way: [image: tab1.png] That is, each line has two elements (represented here by A\d and

Re: GREP pattern to replace the first element with a tab

2021-03-20 Thread John Delacour
On 20 Mar 2021, at 11:30, samar wrote: > I'm looking for something which I assume is pretty easy to accomplish with > GREP but I fail to see how. > > I have a large text file with entries sorted in this way: > > ...That is, the first element is replaced by a tab character *if* the first > e

Re: GREP pattern to replace the first element with a tab

2021-03-20 Thread samar
Thank you! This is helpful, and the result better than mine was. However, when I run the text filter, not all occurrences of the first element get replaced when necessary: [image: tab3.png] Here a tab should also replace A1 in line 5, A2 in line 12, and A3 in line 18. Thanks samar On Saturday

Re: GREP pattern to replace the first element with a tab

2021-03-20 Thread Christian Boyce
You asked for GREP, and this isn’t GREP, but it still solves your problem. I used AppleScript. Note: this script is written in order to be as easy to follow as possible. It could be improved of course. Basically I’m going down your document a line at a time, comparing what’s on the left side

Re: GREP pattern to replace the first element with a tab

2021-03-20 Thread John Delacour
On 20 Mar 2021, at 16:33, samar mailto:arnet...@bluewin.ch>> wrote: > ...when I run the text filter, not all occurrences of the first element get > replaced when necessary: > > > > Here a tab should also replace A1 in line 5, A2 in line 12, and A3 in line 18. That makes no sense to me. If

Re: GREP pattern to replace the first element with a tab

2021-03-21 Thread Christopher Stone
On 03/20/2021, at 15:48, John Delacour mailto:johndelac...@gmail.com>> wrote: > That makes no sense to me. If the current col1 is identical to col1 of the > previous line, then the value will not be printed; that is the clear logic of > the routine. I cannot reproduce your error. Hey Folks,

Re: GREP pattern to replace the first element with a tab

2021-03-21 Thread samar
The reason you cannot reproduce the error with your file may be that the second column is limited to three different texts (B1, B2, and B3) whereas in mine there are more (up to B7 here, but the script should also work with more than seven): A1B1 A1B2 A1B3 A1B4 A1B5 A1B6

Re: GREP pattern to replace the first element with a tab

2021-03-21 Thread Kaveh Bazargan
Here is a two stage regex solution. Assuming no bullets (•) in your file, Search: ^([^\t]+)\t([^\t]+)\r(?=\1) replace: \1\t\2\r• Search: •[^\t]+ Replace with empty On Sun, 21 Mar 2021 at 08:47, Christopher Stone wrote: > On 03/20/2021, at 15:48, John Delacour wrote: > > That makes no sense to

Re: GREP pattern to replace the first element with a tab

2021-03-21 Thread samar
Hi Chris (For some reason my messages here occur only several hours after I've sent them ...) Thank you very much indeed – your AppleScript works perfectly well here (8 seconds when applied to my file of 8,000 lines), the result is the same as when I run Christian's script. So I can even choos

Re: GREP pattern to replace the first element with a tab

2021-03-21 Thread samar
Wow, thank you, Christian, that works exactly as expected! (I only needed to change the closing quotation mark for the script to run.) This is very helpful – my 8,000-line file was magically modified within 8 seconds. samar On Saturday, March 20, 2021 at 8:13:30 PM UTC+1 ChristianBoyce wrote:

Re: GREP pattern to replace the first element with a tab

2021-03-21 Thread samar
Very clever! I had not thought of using positive lookahead, but this indeed makes GREP work here. The only thing I had to add was a tab character before the closing parenthesis in the first search so that similar A1 values (those beginning in the same way) will be excluded: ^([^\t]+)\t([^\t]+)\

Re: GREP pattern to replace the first element with a tab

2021-03-21 Thread Kaveh Bazargan
Glad it helped Samar. Yes, I am generally afraid of lookaheads and sometime not sure of why we can't just use a pattern and replace it verbatim, but this is a good case for it because you have to ensure you do not select the start of the next line in the grep search. The thing I find hardest is re

Re: GREP pattern to replace the first element with a tab

2021-03-21 Thread Christopher Stone
On 03/21/2021, at 03:39, samar mailto:arnet...@bluewin.ch>> wrote: > The reason you cannot reproduce the error with your file may be that the > second column is limited to three different texts (B1, B2, and B3) whereas in > mine there are more (up to B7 here, but the script should also work with

Re: GREP pattern to replace the first element with a tab

2021-03-21 Thread Bruce Van Allen
On 21 Mar 2021, at 19:37, Christopher Stone wrote: On 03/21/2021, at 03:39, samar > wrote: The reason you cannot reproduce the error with your file may be that the second column is limited to three different texts (B1, B2, and B3) whereas in mine there are more (up

Re: GREP pattern to replace the first element with a tab

2021-03-22 Thread Tim A
Neat. Any way to single step thru Find/Replace to watch the the grep pattern progress instead of doing a Replace All? If I do a Next and Replace it doesn't work properly. Thanks On Sunday, March 21, 2021 at 5:42:16 AM UTC-7 Kaveh wrote: > Here is a two stage regex solution. Assuming no bullet

Re: GREP pattern to replace the first element with a tab

2021-03-22 Thread Kaveh Bazargan
Try removing the ^ at the start of search. This is because once you have placed the bullet, the next char is no longer at the start of line. now you will just be searching from the bullet forwards. So search is now: ([^\t]+)\t([^\t]+\r)(?=\1) I also put the last \r inside the second bracketed tex

Re: GREP pattern to replace the first element with a tab

2021-03-25 Thread John Delacour
On 21 Mar 2021, at 08:39, samar wrote: > The reason you cannot reproduce the error with your file may be that the > second column is limited to three different texts (B1, B2, and B3) whereas in > mine there are more (up to B7 here, but the script should also work with more > than seven): Th