Sorting fields in a single line of text? 
Author Message
 Sorting fields in a single line of text?

I've got an interesting problem (at least, it's interesting to me).

 From within a text editor (vi or vile in my case) I want to sort
a line of comma separated text fields alphabetically.  I want to
start the sort from the position of the cursor to the end of the
line.  Here's a sample line of text:

projects:*:1004:betty,john,andy,joe,alice

I want to sort from betty to the end of the line.  I've tried positioning
the cursor on the 'b' in betty and entering ":.,$!sort -t,"
but that's not working (I'm not suprised, but I figured it was worth a shot).

I realize now that the sort command is line oriented and the '-t' flag
is only used to specify which field to sort the lines on.  

Is there a one-liner perl command that takes advantage of perl's
built in sort command that will accomplish this?

thx...

--

Systems Administrator          
Silicon Graphics, Inc.          Tel (415)390-4120       Fax (415)390-6174
"Comparing Dos to UNIX is like comparing a mix-master to a chef's kitchen"



Wed, 19 Jun 1996 11:36:03 GMT  
 Sorting fields in a single line of text?

 J> I've got an interesting problem (at least, it's interesting to me).

 J>  From within a text editor (vi or vile in my case) I want to sort
 J> a line of comma separated text fields alphabetically.  I want to
 J> start the sort from the position of the cursor to the end of the
 J> line.  Here's a sample line of text:

 J> projects:*:1004:betty,john,andy,joe,alice

 J> I want to sort from betty to the end of the line.  I've tried positioning
 J> the cursor on the 'b' in betty and entering ":.,$!sort -t,"
 J> but that's not working (I'm not suprised, but I figured it was worth a shot).

 J> I realize now that the sort command is line oriented and the '-t' flag
 J> is only used to specify which field to sort the lines on.  

 J> Is there a one-liner perl command that takes advantage of perl's
 J> built in sort command that will accomplish this?

 J> thx...

If you just want to sort a list of comma-separated fields, you can do

perl -ne 'chop; $,=","; print (sort split (/,/), "\n");'

This reads a line, splits on commas, sorts the pieces, and prints
them back out again.  The $, tells perl to use commas to separate the
elements printed.

If you don't want to have to manually move to the beginning of the list,
and it is always the last of the colon-separated fields, you can do
this:

perl -ne '/(.*:)(.*)/; print ($1, join (",", sort split (/,/, $2)), "\n");'

This does the same as the above, but operates only on the last
colon-separated element.

I am sure there are many more concise and/or efficient ways to do this,
but this works.  I don't know vi very well; these solutions assume that
the command you gave sends the line with a newline to the program, and
expects a line with a newline back.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If God had wanted us to be concerned for the plight of toads, he would
have made them cute and {*filter*}.
                                                               -- Dave Barry



Thu, 20 Jun 1996 02:42:51 GMT  
 Sorting fields in a single line of text?


Quote:
> From within a text editor (vi or vile in my case) I want to sort
>a line of comma separated text fields alphabetically.  I want to
>start the sort from the position of the cursor to the end of the
>line.  Here's a sample line of text:

>projects:*:1004:betty,john,andy,joe,alice

>I want to sort from betty to the end of the line.

Here is a vi macro that does what you want.  It works with vi
under ultrix.  I have not tested it on other systems.  Your
mileage may vary.  Note: ^M means control-M.  Same goes the
other characters preceded by a caret.

map v mmi^M^[A,^[:.s/,/,^V^V^M/g^M'm+!}sort^M'm}:'m,.j!^M$x

Play-by-play description:

mm        -- mark the line as m
i^M^[     -- break the line at the cursor, because we do not want
             any changes before that point
A,^[      -- append a comma at the end of the line, so that each
             field ends in a comma, including the last.
:.s/,/,^V^V^M/g^M -- break up the fields into one-per-line fragments
'm+       -- go to the first field
!}sort^M  -- sort the fields
'm}       -- go to the last field, carefully!
:'m,.j!^M -- join the fields back into one like
$x        -- delete the trailing comma

--



Thu, 20 Jun 1996 03:04:39 GMT  
 Sorting fields in a single line of text?

I've got an interesting problem (at least, it's interesting to me).

From within a text editor (vi or vile in my case) I want to sort
a line of comma separated text fields alphabetically.  I want to
start the sort from the position of the cursor to the end of the
line.  Here's a sample line of text:

projects:*:1004:betty,john,andy,joe,alice

I want to sort from betty to the end of the line.  I've tried positioning
the cursor on the 'b' in betty and entering ":.,$!sort -t,"
but that's not working (I'm not suprised, but I figured it was worth a shot).

I realize now that the sort command is line oriented and the '-t' flag
is only used to specify which field to sort the lines on.  

Is there a one-liner perl command that takes advantage of perl's
built in sort command that will accomplish this?

thx...
--

Systems Administrator          
Silicon Graphics, Inc.          Tel (415)390-4120       Fax (415)390-6174
"Comparing Dos to UNIX is like comparing a mix-master to a chef's kitchen"



Sat, 22 Jun 1996 04:10:55 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Sorting multiple-line data to single line data

2. Deleting and or appending a single line in a text file - newbie question

3. Sorting a Tab Delimited Text File By Second Field

4. How to put each line of a text area into a field

5. help - 3 magic lines sorting large text

6. sortlen -- filter to sort text by line length

7. I want to convert a mutli-line list to a single line

8. single line regex and multi-line regex without resetting $/

9. QUESTION: rewriting a single line as several shorter lines

10. cr's in input text fields screwing up the database (not text area)

11. How can I attach spaces before a text field or text area

12. using split to grab a single field

 

 
Powered by phpBB® Forum Software