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
Korn: How to zero fill df output so it will sort properly shew01 Shell Programming and Scripting 9 06-04-2008 11:34 AM
how to arrange 3 file to one using awk...? penyu Shell Programming and Scripting 9 01-17-2008 05:26 AM
sort output funksen Shell Programming and Scripting 9 01-14-2008 04:40 AM
Sort - only one field appears in output? miwinter UNIX for Dummies Questions & Answers 1 07-24-2006 11:47 AM
re arrange the columns ahmedwaseem2000 Shell Programming and Scripting 0 09-23-2005 03:51 AM

Closed Thread
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 11-12-2007
llsmr777 llsmr777 is offline
Registered User
  
 

Join Date: May 2007
Posts: 58
how to sort and arrange an output

ok so I have a list of names that end in either ot,om,oa.

So for example

DETOT
MANOA
DET0M
DET0A
MANOT
SEAOT

etc...

I want to be able to group this list by OT, OM, OA and have the output have some headers like this and be alphabatized and if possible be in colums instead of like below so three columns:

OT
-----
DETOT
MANOT
SEAOT

OA
----
DETOA
MAN0A

Thanks for any feedback!
  #2 (permalink)  
Old 11-12-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 717
Hi.

Assuming that the words can be any length, I think I would reverse the lines, sort them, then reverse them again just before printing. Check to see if you have command rev, then look at command sort ... cheers, drl

PS I think some zeros, "0", are mixed in with letter ohs, "O" in the sample data.
  #3 (permalink)  
Old 11-12-2007
llsmr777 llsmr777 is offline
Registered User
  
 

Join Date: May 2007
Posts: 58
Thank you for the reply. Actually it will always be 5 charectors and it's all the letter O not zero's......

Is your scenerio still the best way? How do I column it out? i'm assuming some printf command but again I'm so new I'm not familiar on how to use it.
  #4 (permalink)  
Old 11-12-2007
llsmr777 llsmr777 is offline
Registered User
  
 

Join Date: May 2007
Posts: 58
what about if I add a grep statement that greps out OT. Then I run it again to grep out OM and then again for OA....

How would I then go about printing each output into a column with a header?

Thanks!
  #5 (permalink)  
Old 11-12-2007
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
  
 

Join Date: Nov 2007
Location: New Zealand
Posts: 922
Quote:
Originally Posted by llsmr777 View Post
ok so I have a list of names that end in either ot,om,oa.

So for example

DETOT
MANOA
DET0M
DET0A
MANOT
SEAOT

I want to be able to group this list by OT, OM, OA and have the output have some headers like this and be alphabatized and if possible be in colums instead of like below so three columns:
Since you already know what groups you are expecting, you can probably skip the step of reversing the lines and just grep out the relevant bits thus:

Code:
#!/bin/sh
MYGROUPS="OT OM OA"
file_to_sort=$1
for group in $MYGROUPS
do
    echo "$group"
    echo "--"
    egrep "${group}\$" $file_to_sort | sort
    echo ""
done
# Catch any unexpected input (if you want that)
if egrep -v "`echo $MYGROUPS | sed 's/ /\|/g'`" $file_to_sort > /dev/null
then
    echo "*** UNEXPECTED ***"
    egrep -v "(`echo $MYGROUPS | sed 's/ /\|/g'`)" $file_to_sort
fi

  #6 (permalink)  
Old 11-12-2007
llsmr777 llsmr777 is offline
Registered User
  
 

Join Date: May 2007
Posts: 58
Ok i'm really really new to this is there anyway you can break down your post?

This is what I understand.

is to echo each group and put a -- under it
then search for each group (OT OM OA) and I kinda get lost here ....

I'm so sorry that I need so much help!

by the way I did test it and it worked out perfect!!! Is there a way to make the outputs be side by side columns?
Also how can I change the headings of the groups? So I want to still group by OT OM OA but I want the headings of each to be something different?

Last edited by llsmr777; 11-12-2007 at 05:48 PM..
  #7 (permalink)  
Old 11-12-2007
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
  
 

Join Date: Nov 2007
Location: New Zealand
Posts: 922
Quote:
Originally Posted by llsmr777 View Post
Is there a way to make the outputs be side by side columns?
Side by side is harder as you'd have to either make temporary files or store the lists in memory, then display it back out once you are ready.

Shell doesn't lend itself very well to formatting output, you'd have to monkey about with inserting a number of tab characters based on how long the various names in the file are.

Perl would probably be the language of choice for this as it's got a very nice function for generating reports etc called 'format'. You can design a form, then populate the data and dump it out via the 'write' command. It'd be a bit more complex than the previous shell script though as it would also need to store things in memory or in files rather than streaming everything.
Closed Thread

Bookmarks

Tags
regex, regular expressions

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 12:00 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