Solved

Sorting in UNIX

Posted on 2001-06-21
3
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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 find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses

623 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