The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sort columns by field kamel.seg Shell Programming and Scripting 4 02-20-2008 02:50 AM
how to numeric sort on field time rahulspatil_111 Shell Programming and Scripting 1 04-27-2007 08:52 AM
Sort - only one field appears in output? miwinter UNIX for Dummies Questions & Answers 1 07-24-2006 07:47 AM
field separator as regexp Abhishek Ghose Shell Programming and Scripting 2 02-15-2006 04:49 PM
how to include field separator if there are blank fields? ReV Shell Programming and Scripting 19 07-13-2005 01:50 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-26-2005
SSteve's Avatar
Registered User
 

Join Date: Apr 2005
Location: Sierra Foothills, California
Posts: 15
How do I specify tab as field separator for sort?

I'm trying to use sort on a file with tab-delimited fields. I can't figure out how to tell sort to use the tab character as the field separator. I'm trying this on both an HP Unix system and on OS X (using bash on both).

Things I've tried:
sort -t\t
sort -t"\t"
sort -t\"\t\"

I've tried other things out of desperation, but nothing has worked so far.
I've looked through all the messages on the forum but couldn't find an answer. I found one message where someone was using the third form above, but that didn't work for me.

TIA,
-Steve
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-26-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,002
sort -t `/bin/echo '\t'`
Reply With Quote
  #3 (permalink)  
Old 04-26-2005
SSteve's Avatar
Registered User
 

Join Date: Apr 2005
Location: Sierra Foothills, California
Posts: 15
Thanks for the quick reply, but that doesn't seem to work. Here's a sample input file where I've converted the tabs to vertical bars:

a album|c song|2
b album|d song|2
a album|a song|4
a album|b song|1
b album|z song|1
b album|a song|3
a album|f song|3
b album|c song|4

(The real file is an exported playlist from iTunes. This is a simplified test version.) I want to sort by album, then track number. With this file, sort -t\| -k 1,1 -k 3,3n works fine. But with the real file that contains tabs, nothing works. I tried sort -t `/bin/echo '\t'` -k 1,1 -k 3,3n but it sorts by the entire line just like everything else I've tried.
Reply With Quote
  #4 (permalink)  
Old 04-26-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,002
sort -t "`/bin/echo '\t'" -k 1,1 -k 3,3n
Reply With Quote
  #5 (permalink)  
Old 04-26-2005
SSteve's Avatar
Registered User
 

Join Date: Apr 2005
Location: Sierra Foothills, California
Posts: 15
Thanks but still no luck. On OS X that still sorts by the entire line. On the HP system, it's rejected as invalid syntax. (I removed the character after the opening double quote since that must be a typo.)
Reply With Quote
  #6 (permalink)  
Old 04-26-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,002
sorry for the typo!

does this sort by the first field?
sort -t "`/bin/echo '\t'`" -k 1,1
Reply With Quote
  #7 (permalink)  
Old 04-26-2005
SSteve's Avatar
Registered User
 

Join Date: Apr 2005
Location: Sierra Foothills, California
Posts: 15
Ok, "`/bin/echo '\t'`" did it! Thanks very much for your help and perseverance.

I'm not familiar with the "backwards" single quote. What does that do? (And what do you call it?)
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:20 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0