The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reversing file order using SED MBGPS Shell Programming and Scripting 3 01-07-2009 10:10 AM
creating a file in reverse order of another file srilaxmi Shell Programming and Scripting 3 11-28-2008 12:54 AM
sort a file in reverse order frustrated1 Shell Programming and Scripting 11 09-21-2005 04:41 PM
look in file, seperate letters, put in order... chekeitout UNIX for Advanced & Expert Users 3 11-05-2004 05:00 PM
Sorting filenames by order in another file samudimu UNIX for Advanced & Expert Users 4 05-24-2002 01:03 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-11-2009
sniper57 sniper57 is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 17
Question sed or awk to order a file

Hi -

I have a file with lots of lines in that I need to order based on the number of commas!

e.g the file looks something like :-
cn=john,cn=users,cn=uk,dc=dot,dc=com
cn=john,cn=users,dc=com
cn=users,cn=groups,dc=com
cn=john,cn=admins,cn=users,cn=uk,dc=dot,dc=com
cn=fred,cn=users,cn=uk,dc=dot,dc=com
cn=simon,cn=users,cn=uk,dc=dot,dc=com
cn=users,dc=com

I need to order the file so that it ends up like :-
cn=john,cn=admins,cn=users,cn=uk,dc=dot,dc=com
cn=john,cn=users,cn=uk,dc=dot,dc=com
cn=fred,cn=users,cn=uk,dc=dot,dc=com
cn=simon,cn=users,cn=uk,dc=dot,dc=com
cn=john,cn=users,dc=com
cn=users,cn=groups,dc=com
cn=users,dc=com

I am hoping someone knows some sed or awk that can do this ?

Many thanks
  #2 (permalink)  
Old 06-11-2009
rakeshawasthi rakeshawasthi is offline
Registered User
  
 

Join Date: Aug 2004
Location: India
Posts: 379
There could be a better way to do this, but this may help...

Code:
awk -F"," '{print NF ":" $0}' datafile | /bin/sort -t: +0 -1 -n -r | awk -F":" '{print $2}'

  #3 (permalink)  
Old 06-11-2009
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,926
If Perl is acceptable:


Code:
perl -e'print sort{$b=~tr/,//<=>$a=~tr/,//}<>' infile

Otherwise:


Code:
awk -F, '{ print NF "\t" $0 }' infile | 
  sort -rn | 
    cut -f2-

GNU awk:


Code:
WHINY_USERS=oops awk -F, 'END {
  for (n in f) s[i++] = f[n]
  while (i--) print s[i]
  }  
{ f[NF] = NF in f ? f[NF] RS $0 : $0 }
' infile


Last edited by radoulov; 06-11-2009 at 06:50 AM..
  #4 (permalink)  
Old 06-11-2009
sniper57 sniper57 is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 17
Great answers ... thanks for the help
  #5 (permalink)  
Old 06-12-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,098

Code:
my @lines=<DATA>;
print map {$_->[0]}
sort {$b->[1] <=> $a->[1]}
map {[$_,tr/,//]} @lines;
__DATA__
cn=john,cn=users,cn=uk,dc=dot,dc=com
cn=john,cn=users,dc=com
cn=users,cn=groups,dc=com
cn=john,cn=admins,cn=users,cn=uk,dc=dot,dc=com
cn=fred,cn=users,cn=uk,dc=dot,dc=com
cn=simon,cn=users,cn=uk,dc=dot,dc=com
cn=users,dc=com

Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:24 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0