If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Sort rows

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-10-04, 08:24
yahoo yahoo is offline
Registered User
 
Join Date: Jun 2004
Posts: 6
Sort rows

I have a file like below:

gina, crab.george, john, joe, crab.mckay

Get rid of the 'crab.' and sort the rest of the output. I expect the output to be like:

george, gina, joe, john, mckay

Any help appreciated.
Reply With Quote
  #2 (permalink)  
Old 06-10-04, 12:35
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
This smells like homework (and quite a cute test too)...

Anyhow, here's one way. Use 'sed' to remove crab, the commas and to add a newline. You now have your input as a column, which 'sort' can sort. Then use awk to re-add the commas and transform the column back to a row.

Code:
echo "gina, crab.george, john, joe, crab.mckay" | sed 's/crab\.//g;s/, */\
/g' | sort | awk '{printf (comma"%s",$0); comma=","}'
Damian
Reply With Quote
  #3 (permalink)  
Old 06-11-04, 06:00
yahoo yahoo is offline
Registered User
 
Join Date: Jun 2004
Posts: 6
Hi Damian,

Oops sorry but this fails.

When I execute:

echo "gina, crab.george, john, joe, crab.mckay" | sed 's/crab\.//g;s/, */\/g' | sort | awk '{printf (comma"%s",$0); comma=","}'

It says:

sed: command garbled: s/crab\.//g;s/, */

Am I missing out on something or doing something wrong.

Yahoo
Reply With Quote
  #4 (permalink)  
Old 06-11-04, 06:02
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
No, you ARE doing something wrong...

The newline that I included in the sed command (after the '\') is necessary.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On