Text File Data Clean-up with TextWrangler

Hi there,

I’m writing a script that applies various find and replace operations on a single text file using TextWrangler.
It’s my first time experimenting with GREP regular expressions and I’m struggling to get the script to find the data I’m wanting to process.

The text file consists of lines like this:

The vertical bars separate the data into columns. The goal is to create a find and replace rule that will find a column containing
“C/O 6.99”, remove the "C/O " and move just the numbers to the right most column, which will be empty.

So, for clarity:

This will need to be able to search out and alter similar lines that contain different numerical values which may
contain 2, 1 or 0 decimal places and whole values of up to 3 digits.
So the numbers could range from “999.99” to “1” or even just the decimal e.g. “.89”
Also sometimes the number will be preceded by a “£” as they are product prices, but this does not always appear.
The “C/O” always appears.

I’ve been using the standard TextWrangler ‘replace’.

I’m not sure if you’d class this as an AppleScript or TextWrangler post but I’m running out of places to ask!

Any advice or suggestions would be appreciated.

Thank you in advance!

-Paul

The best place to ask this sort of question is the BBEdit list on Google groups.

To do it with the BB/TW search dialog, set grep on and wrap-around on
in the dialog, then

Find:
(.+|)C/O ([£\d.]+)(.+)(||)$

Replace
\1\3|\2|

I’d suggest you join that group if you want to learn more about using regular expressions
in TW or BBEdit”which are essentially the same.

JD

Hi, Paul. Welcome to MacScripter.

An alternative to John’s search and replace expressions would be .

Find:
C/O ([^|]+)(|[^|]+|)

Replace:
\2\1

. ie. lose the C/O (assumed to be unique in each line) and swap whatever remains in that barred section with the whole of the following barred section (including the bars).

You can do this in the front document with AppleScript like so:

set searchPattern to "C/O ([^|]+)(\\|[^|]+\\|)"
set replacePattern to "\\2\\1"

tell application "TextWrangler"
	tell text window 1
		replace searchPattern using replacePattern options {search mode:grep, wrap around:true}
	end tell
end tell

Exactly what I was looking for. I’ve been struggling to find assistance with this for a while.

Thank you both for the advice and examples! :smiley: