how to sort and arrange an output


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to sort and arrange an output
# 1  
Old 11-12-2007
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  
Old 11-12-2007
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  
Old 11-12-2007
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  
Old 11-12-2007
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  
Old 11-12-2007
Quote:
Originally Posted by llsmr777
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  
Old 11-12-2007
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  
Old 11-12-2007
Not a problem Smilie
First section is just to move the list of groups we are splitting into tp the top in case you want to change it later.
We then take a command line argument for the file to search over and store it.
Code:
#!/bin/sh
MYGROUPS="OT OM OA"
file_to_sort=$1

Next we perform the loop below for each group (ie once per item in $MYGROUPS)
The egrep (a version of grep that understand regular expressions better) is looking for lines that end in our group identifier (the '\$' means 'end of line') in the file. The list that comes out is then passed to sort to alphabetise it.
Code:
for group in $MYGROUPS
do
    echo "$group"
    echo "--"
    egrep "${group}\$" $file_to_sort | sort
    echo ""
done

The next part it completely optional, I just included it in case you might want to throw up a warning listing any lines that didn't match one of the groups you are looking for. You could add an 'exit 1' line just before the 'fi' at the end if you want to have it produce an error status when this happens too.

The first egrep is looking to see if there is any output left after grepping out (via the -v flag) any of the groups we are looking for.
It builds the regular expression based on the MYGROUPS variable by replacing the spaces with '|' symbols. This way it is doing an egrep -v "(OT|OM|OA)\$" over the file (ie list all output that doesn't end with Ot, OM or OA).
The first time is to see if we find any (it just throws it away), if so, it prints the header than does it again to the screen. It's a shame to do the search twice, you could store the output from the first run and just display it later but I don't like generating temporary files and was being a bit lazy Smilie
Code:
# 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

I've actually seen a couple of minor mistakes I made in that last part too - fixed in teh above version. The first grep was missing the brackets to group the regular expression and both greps were missing the \$ to indicate that it should be looking at the end of the line.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Any way to sort ps output based on STIME?

Hi, This is one of the thing that am looking for when I post the question on the ps wrapper. It has since been closed as it has taken me too long to post an example. I have replaced some of the original content of the ps output. uname -a = SunOS <hostname> 5.11 11.3 sun4v sparc sun4v ... (1 Reply)
Discussion started by: newbie_01
1 Replies

2. Linux

Arrange output of a command

Hi, I am using a command :- ps -auxwww 2> /dev/null | awk 'match ($0, GIN:*/) {print substr($0, RSTART+3, RLENGTH-3)}' | sort | grep -v -F 'which gives output as 3 aruau 5 asun 4 cgan Now I need a command which gives me something like this :- 3 aruau 5 asun 4 cgan... (2 Replies)
Discussion started by: Raj999
2 Replies

3. Shell Programming and Scripting

Need to sort an output

with a command i get a long list (example) and the entrys are intended. DName=AAAAA DName=AAAAA lba=838,860,800 label=ftw_1 DName=BBBBB DName=BBBBB lba=838,860,800 label=ftw_2 DName=CCCCC ... (3 Replies)
Discussion started by: Serano
3 Replies

4. Shell Programming and Scripting

sort ps output in seconds

Now I van sort in hour-minute-second. I need in seconds ps -eo pid,etime,args --sort=start_time | grep bash Sample Output 15064 03:23 -bash I need in 03:23 in seconds (1 Reply)
Discussion started by: anil510
1 Replies

5. Shell Programming and Scripting

Sort and output to different files

Hi all! I have a comma delimited file and I'm sorting it based on fields 6, 8 and 10. The following does the job: sort -t, -nk6,10 unsorted.txt -o sorted.txt What I need to do now is to write every row containing same values on fields 6, 8 and 10 to a different file. Any ideas on how to do... (2 Replies)
Discussion started by: Tr0cken
2 Replies

6. Shell Programming and Scripting

Sort output is different with Corn

Hi, I am sorting a file, but getting different output with crontab. input file-file z Z x X y Y when I sort manually sort file x X y Y z Z (4 Replies)
Discussion started by: suresh3566
4 Replies

7. Shell Programming and Scripting

Arrange output based on rows into columns

Hi All, I would like to ask help on how can i achieve below output. Inputfile: Oct11,apa1-daily,01:25:01 Oct11,apa2-daily,01:45:23 Oct12,apa1-daily,02:30:11 Oct12,apa2-daily,01:55:01 Oct13,apa1-off,01:43:34 Oct13,apa2-off,01:22:04 Desired output: Clients ... (3 Replies)
Discussion started by: mars101
3 Replies

8. UNIX for Dummies Questions & Answers

Sort fdupes output by size

Hi I have a file that is a fdupes output. I'd like to sort the duplicated file by size. The format file is the following: 5996544 bytes each: /path1/to/file1.jpg /path2/to/file1.jpg /pathx/to/file1.jpg ... random number of lines /path999/to/file1.jpg 591910 bytes each:... (2 Replies)
Discussion started by: AdminLew
2 Replies

9. Shell Programming and Scripting

sort output

this one is a bit tricky: host16 /etc/passwd changed Thu Jan 3 16:39:48 host3 /etc/group changed Thu Jan 3 16:39:48 host2 /etc/services changed Thu Jan 3 16:39:48 host10 /etc/group changed Thu Jan 10 09:59:01 host11 /etc/group changed Thu Jan 3 18:55:38 host17 /etc/group changed Thu Jan... (9 Replies)
Discussion started by: funksen
9 Replies

10. UNIX for Dummies Questions & Answers

Sort - only one field appears in output?

I'm running against a file with 1008 records like this, all beginning '4760 Slave': 4760 Slave,7,3607 ,GL ,200605,11320024 , ,GBP ,X00033 ,AI80190 ... (1 Reply)
Discussion started by: miwinter
1 Replies
Login or Register to Ask a Question