Solved

Sorting in UNIX

Posted on 2001-06-21
3
304 Views
Last Modified: 2013-12-06
I'm using HP UX, kornshell.

How do you sort by position and not fields?

File to sort
file name: test.dat

Joe  sdfwere adf dsf dsf e    78323
Jack dsf asdfkj ajsd ljdf     23432
Mary asd asdfj sdfj    fsd ad 12343
Able asdf sdfjjfdljsl sdkjf   27382

I want to sort by the last 5 digits of each record.

How do I do this?

Thanks
0
Comment
Question by:waipahu
3 Comments
 
LVL 6

Accepted Solution

by:
bira earned 100 total points
ID: 6216447
If the last 5 characters are aligned, you can use that:
 cat test.txt |sort -k.31,.35  
 So only the columns between 31 and 35 would be sorted.

 But if the last 5 characters are not aligned, you
 better use a C program.
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 6216453
If the last columns are not aligned, try something like:

cat test.txt | awk '{ print $NF" "$0}' | sort -n | awk '{ print substr($0, 7, length($0) - 6) }'
0
 

Author Comment

by:waipahu
ID: 6216572
Thank you very much! It works!
Thanks also for the AWK command!
You guys are great!

Mahalo! (thank you in hawaiian)

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question