Sponsored Content
Full Discussion: sort output
Top Forums Shell Programming and Scripting sort output Post 302157567 by drl on Friday 11th of January 2008 09:25:14 AM
Old 01-11-2008
Hi.

A POSIX sort should work. Here's an example from Linux:
Code:
sort -k 8,8n -k 5,5M -k 6,6n -k 7,7

As done in the script:
Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate POSIX sort.

#  ____
# /
# |   Infrastructure BEGIN

set -o nounset
echo

debug=":"
debug="echo"

## The shebang using "env" line is designed for portability. For
#  higher security, use:
#
#  #!/bin/sh -

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash sort column

echo

FILE=${1-data1}
echo " Sample of input file $FILE:"
head -5 $FILE

# |   Infrastructure END
# \
#  ---

echo
echo " Results from processing:"

# Field assignments:
# 1      2           3       4   5   6 7        8
# host16 /etc/passwd changed Thu Jan 3 16:39:48 2007
# ( could be used depending on sort    12 45 78 )

sort -k 8,8n -k 5,5M -k 6,6n -k 7,7 $FILE |
column -t

exit 0

producing:
Code:
% ./s1

(Versions displayed with local utility "version")
GNU bash 2.05b.0
sort (coreutils) 5.2.1
column (local) - no version provided.

 Sample of input file data1:
host16 /etc/passwd changed Thu Jan 3 16:39:48 2008
host3 /etc/group changed Thu Jan 3 16:39:48 2008
host2 /etc/services changed Thu Jan 3 16:39:48 2008
host10 /etc/group changed Thu Jan 10 09:59:01 2008
host11 /etc/group changed Thu Jan 3 18:55:38 2008

 Results from processing:
host3   /etc/security/user  changed  Fri  Dec  21  12:25:23  2007
host6   /etc/group          changed  Thu  Dec  27  10:34:27  2007
host7   /etc/security/user  changed  Thu  Dec  27  10:34:27  2007
host8   /etc/passwd         changed  Thu  Dec  27  11:50:40  2007
host5   /etc/passwd         changed  Thu  Dec  27  12:00:57  2007
host1   /etc/passwd         changed  Thu  Dec  27  12:05:58  2007
host3   /etc/passwd         changed  Thu  Dec  27  12:13:29  2007
host4   /etc/passwd         changed  Thu  Dec  27  12:28:13  2007
host3   /etc/passwd         changed  Thu  Dec  27  12:47:29  2007
host16  /etc/passwd         changed  Thu  Jan  3   16:39:48  2008
host2   /etc/services       changed  Thu  Jan  3   16:39:48  2008
host3   /etc/group          changed  Thu  Jan  3   16:39:48  2008
host17  /etc/group          changed  Thu  Jan  3   17:45:41  2008
host11  /etc/group          changed  Thu  Jan  3   18:55:38  2008
host2   /etc/security/user  changed  Tue  Jan  8   13:15:02  2008
host10  /etc/group          changed  Thu  Jan  10  09:59:01  2008

I often forget to make sure the key specifications include the beginning and end field, e.g. -k 3,3n, to sort on field 3 numerically. If you forget the final ",3", the key will be considered to be from the beginning of field 3 to the end of line.

The inclusion of a collating sequence for months was a very good design feature from the original UNIX sort.

I piped this though column to more easily see the ordering.

See man sort (or better, info coreutils sort, if you have it) for details ... cheers, drl
 

10 More Discussions You Might Find Interesting

1. 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

2. UNIX for Dummies Questions & Answers

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... (10 Replies)
Discussion started by: llsmr777
10 Replies

3. UNIX for Dummies Questions & Answers

How to sort output from find from oldest to newest?

Hi, How do I sort the output of find to provide a listing of files from oldest to newest? For example, if I do a find /tmp -type f -print I want the output to be sorted in the order of the oldest to the newest file. Thanks in advance. (2 Replies)
Discussion started by: newbie_01
2 Replies

4. 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

5. 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

6. 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

7. 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

8. UNIX for Dummies Questions & Answers

Ls output sort on multiple column

Hi All, I have one requirement, where I need to have output of ls -l command sorted on 1) first on filename 2) last modified time ( descending ) - latest change first. I am not able to figure out how to do it.. Also I dont have a way to change Date display for ls -ltr command.. I am... (5 Replies)
Discussion started by: freakabhi
5 Replies

9. 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

10. 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
SHADOW(5)							File Formats Manual							 SHADOW(5)

NAME
shadow - encrypted password file DESCRIPTION
shadow contains the encrypted password information for user's accounts and optional the password aging information. Included is Login name Encrypted password Days since Jan 1, 1970 that password was last changed Days before password may be changed Days after which password must be changed Days before password is to expire that user is warned Days after password expires that account is disabled Days since Jan 1, 1970 that account is disabled A reserved field The password field must be filled. The encryped password consists of 13 to 24 characters from the 64 character alphabet a thru z, A thru Z, 0 thru 9, . and /. Refer to crypt(3) for details on how this string is interpreted. The date of the last password change is given as the number of days since Jan 1, 1970. The password may not be changed again until the proper number of days have passed, and must be changed after the maximum number of days. If the minimum number of days required is greater than the maximum number of day allowed, this password may not be changed by the user. An account is considered to be inactive and is disabled if the password is not changed within the specified number of days after the pass- word expires. An account will also be disabled on the specified day regardless of other password expiration information. This information supercedes any password or password age information present in /etc/passwd. This file must not be readable by regular users if password security is to be maintained. FILES
/etc/passwd - user account information /etc/shadow - encrypted user passwords SEE ALSO
chage(1), login(1), passwd(1), su(1), passwd(5), pwconv(8), pwunconv(8), sulogin(8) AUTHOR
Julianne Frances Haugh (jockgrrl@ix.netcom.com) SHADOW(5)
All times are GMT -4. The time now is 12:00 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy