Can I do a 'sort lines...' based on how many times a particular character
appears in the line?

Not directly.
I have several files of street addresses that will eventually end up in a
single csv. Some lines have more separating commas than others and I'd like
to sort each file according to how many commas appear in a line.

Even better would be to use just the commas that appear after the final \ as
in the example below:

CD 6\C\Company, Unit 2 Little Place, Clarke Road N10\Company, Unit 2 ,Little
Place, Clarke Road ,N10
CD 8\L\Flat C, 64 Lime Road N11\Flat C, 64 ,Lime Road ,N11
CD 7\B\Broad Street N6\Broad Street ,N6

I suggest you first translate the file to start with <count of commas><tab>, then sort numerically, then remote the leading count.

Adding the count of the number of commands after the last \ is done with perl like this:

perl -i.bak -n -e 'm/.*\\(.*)/; print $1 =~ tr/,/,/, "\t", $_;' file.txt

Broken down, it means:

-i - read and then write that input file
-i.bak - saving a backup copy with extension .bak
-n reading each line but not printing anything
-e with script that follows
m/.*\\(.*)/ find everything after the last \
print print the following
$1 =~ tr/,/,/ - translate in $1 every comma to a comma, returning the number
"\t" tab
$_ the line, including trailing linefeed (make sure your file is linefeed line endings)
file.txt the name of the input/output file

becomes

CD 7\B\Broad Street N6\Broad Street ,N6
CD 8\L\Flat C, 64 Lime Road N11\Flat C, 64 ,Lime Road ,N11
CD 6\C\Company, Unit 2 Little Place, Clarke Road N10\Company, Unit 2 ,Little
Place, Clarke Road ,N10

Becomes:

2 CD 6\C\Company, Unit 2 Little Place, Clarke Road N10\Company, Unit 2 ,Little
2       Place, Clarke Road ,N10
3       CD 8\L\Flat C, 64 Lime Road N11\Flat C, 64 ,Lime Road ,N11
1       CD 7\B\Broad Street N6\Broad Street ,N6

Then sort numerically (sort -n or use BBEdit)
Then remove the leading <number><tab> (left as an exercise).

Enjoy,
   Peter.

--
Check out Interarchy 8, just released.
<http://www.stairways.com/>  <http://download.stairways.com/>

--
------------------------------------------------------------------
Have a feature request? Not sure the software's working correctly?
If so, please send mail to <[EMAIL PROTECTED]>, not to the list.
List FAQ: <http://www.barebones.com/support/lists/bbedit_talk.shtml>
List archives: <http://www.listsearch.com/BBEditTalk.lasso>
To unsubscribe, send mail to:  <[EMAIL PROTECTED]>

Reply via email to