Reputation: 11
The goal is to select and cut the second column and then paste it to the left of the first column, which these next 3 commands do perfectly:
cut selection
select insertion point before first character of second line
paste column characters
BBEdit rectangular/columnar text selection
The other method is to do this row by row, but this is much slower than a rectangular selection followed by a Cut and a Paste Column.
Input example:
"Key":"A0Qk9DVpUkGulGpHxr+JlW4=","PageNumber":1
"Key":"AEcL8VGZFEUCeSx5vvCxto0=","PageNumber":2
"Key":"ABVH60Q2pc+AePwQNAdmMdQ=","PageNumber":3
"Key":"APqXJ0lwaCrLtVJlbBnc6mI=","PageNumber":4
"Key":"ABcrwNYHkx+qPTbzuUHlYEI=","PageNumber":5
"Key":"AJZ+S/Y9PJ2o42lqMnPlAN4=","PageNumber":6
"Key":"AAWHU3+jHUoMqLsJ4s4zQMs=","PageNumber":7
"Key":"ANG5KxVCEToVhf9gDKcM93E=","PageNumber":8
"Key":"AL3A2YbsAuQvUPWZDHTlssE=","PageNumber":9
"Key":"AH8buWygY/hZ9VHnoyMuIEs=","PageNumber":10
"Key":"AHZNQ3E90ySyEiwQZWBoEuc=","PageNumber":11
"Key":"AJHkHd/ZVdUZIFo4FcYwXnM=","PageNumber":12
"Key":"AEbWD9Hsrl9Uig+p0am6Rj4=","PageNumber":13
"Key":"APE5vIEkT0Y3/2Tv/ElfJiA=","PageNumber":14
"Key":"ALG40gmAwr/id3DJnczzuSE=","PageNumber":15
"Key":"ABeop321v++Ki3T8uc7ouuM=","PageNumber":16
"Key":"ABgh96CHbwrASAVqo/Nqtf4=","PageNumber":17
"Key":"AF6+J/oF4We4Cjumo+b34eY=","PageNumber":18
"Key":"A0BLp58WhLAlt+ArrbJ52vQ=","PageNumber":19
"Key":"AIDBCaRs3y9nkZJMwMx8HTg=","PageNumber":20
The output should follow this form:
"PageNumber":1,"Key":"AOQk9DVpUkGulGpHxr+J1W4="
"PageNumber":2,"Key":"AEcL8VGZFEUCeSx5vvCxto0="
"PageNumber":3,"Key":"ABVH60Q2pc+AePwQNAdmMdQ="
Upvotes: 1
Views: 876
Reputation: 1205
BBEdit has built-in tools to perform this search and replace using the 'grep' option. To find out about this, they have a great reference: Help > BBEdit Help, then choose 'Grep Reference'. In essence, it generalises the text of the document and when it finds that text, breaks it into blocks which you can re-order when you replace them.
In this case (using the front document in bbedit), it looks for "Key": and then a string of 23 characters followed by =" and makes this block 1. Next it looks for "PageNumber": followed by an unspecified number of digits which is made into block 2. In the replace section, it reverses their order and inserts a comma between them.
You can preview the search in bbedit's 'find' dialogue with the following in the find and replace areas:
Find:
("Key":".{23}="),("PageNumber":\d+)
Replace:
\2,\1
In an applescript, the find/replace will look like this:
tell application "BBEdit"
replace "(\"Key\":\".{23}=\"),(\"PageNumber\":\\d+)" using "\\2,\\1" searching in text 1 of text document 1 options {search mode:grep, starting at top:true}
activate
end tell
It is important to note that applescript requires that quotes and backslashes be 'escaped' with a \
character and they have been added above. I have included both so you can test the same search from applescript or directly in bbedit.
As an alternative, you can also re-order the text using a shell command, such as 'sed'. Assuming that the input text is a file named 'input.txt' on the desktop (with the same contents as the bbedit document), the following command would re-order the text and save it in a file named 'output.txt' on the desktop.
set dk to POSIX path of (path to desktop as text)
set inputFile to quoted form of (dk & "input.txt" as text)
--> "'/Users/username/Desktop/input.txt'"
set outputFile to quoted form of (dk & "output.txt" as text)
--> "'/Users/username/Desktop/output.txt'"
do shell script "sed -E 's/(\"Key\":\".{23}=\"),(\"PageNumber\":[[:digit:]]+)/\\2,\\1/' <" & inputFile & ">" & outputFile
--> "PageNumber":1,"Key":"A0Qk9DVpUkGulGpHxr+JlW4="
Upvotes: 2