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 of the tab to the value that was in Column One the last time it
changed. If it’s the same as that, I don’t write it— I just write tab and
Column Two to a variable called newText. If it’s different, I write Column One
and Column Two to newText. Finally I set the text of the document to the
contents of the variable newText.
Watch out fo the line that sets AppleScript’s text item delimiters to a tab.
The line actually looks like this before compiling:
set AppleScript's text item delimiters to {"\t”}
But when you compile the slash-t is changed to a tab, which is invisible.
--
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "BBEdit"
set myDoc to document 1
set originalText to text of myDoc
set newText to ""
set ColumnOne to ""
set ColumnTwo to ""
--
set theParagraphs to paragraphs of originalText as list
repeat with aParagraph in theParagraphs
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {" “}— that’s a
tab in there— you can’t tell but it is. Use “\t” as I’ve written
— on the next line
-- set AppleScript's text item delimiters to {"\t”}— commented out so you
can see how it looks before compiling
set thisColumnOne to text item 1 of aParagraph
set thisColumnTwo to text item 2 of aParagraph
if thisColumnOne is ColumnOne then
set thisOutput to tab & thisColumnTwo & return
else
set thisOutput to thisColumnOne & tab & thisColumnTwo &
return
set ColumnOne to thisColumnOne
end if
set newText to newText & thisOutput
end repeat
set AppleScript's text item delimiters to saveTID
set text of myDoc to newText
end tell
> On Mar 20, 2021, at 4:30 AM, samar <[email protected]> wrote:
>
> 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:
>
> <tab1.png>
>
> That is, each line has two elements (represented here by A\d and B\d)
> separated by one tab character. The length of the two elements is between 1
> and 100 characters each.
>
> Now I need a GREP Find/Replace string to make it look like this:
>
> <tab2.png>
>
> That is, the first element is replaced by a tab character *if* the first
> element is a repetition of the first element of the previous line(s). The
> sort order remains unchanged.
>
> Sounds simple. And yet all I manage to GREP for is this:
>
> <Screenshot 2021-03-20 at 12.26.11.png>
> The result looks promising but in now way satisfactory:
>
> <Screenshot 2021-03-20 at 12.22.46.png>
>
> The tab characters are inserted correctly, but the problem has to do with
> repetition ... Does anyone see how this can be resolved?
>
> Thanks
> samar
>
>
—
Socially distantly yours,
Christian Boyce
--
This is the BBEdit Talk public discussion group. If you have a feature request
or need technical support, please email "[email protected]" 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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/bbedit/2441499A-F563-4AF4-B63C-8B5B9B5B47DC%40christianboyce.com.