Ls output sort on multiple column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Ls output sort on multiple column
# 1  
Old 11-13-2014
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 working on korn shell env.

Please help me with this.

Thanks
Abhijeet R
# 2  
Old 11-13-2014
If you're sorting by the entire filename then the time isn't going to be relevant - filenames are unique. Presumably you want to sort by part of it?

Provide some sample input and output.
# 3  
Old 11-13-2014
I have files as below and need to be sorted by first by filename and then by timestamp with latest first. In other words I want to group the files and then sort it by timestamp

Code:
-rw-r--r--    1 someuser   somegrp           61 Oct 22 16:01 abc.dat.20141022_160144
-rw-r--r--    1 someuser   somegrp           61 Oct 22 16:04 abc.dat.20141022_160144
-rw-r--r--    1 someuser   somegrp       260434 Oct 22 16:04 mno.dat.20141022_160509
-rw-r--r--    1 someuser   somegrp       761028 Oct 22 16:05 mno.dat.20141022_160509
-rw-r--r--    1 someuser   somegrp         1435 Oct 22 16:06 mno.dat.20141022_160509


Last edited by Corona688; 11-13-2014 at 11:25 AM..
# 4  
Old 11-13-2014
That's tricky sorting afterwards, since ls doesn't output a consistent date format... Sometimes it'll output hours and minutes, sometimes months, sometimes years, but almost never everything you need to sort. ls can sort by itself, but it can't group.

So my approach would be to find all the groups, then list them individually.

Code:
# ls -1, as in one, list in one column
ls -1 |
        # Strip off the .whatever, group in A, then print all unique groups
        awk -F. -v OFS="." '$2 == "dat" { NF-- ; A[$0] } END { for(X in A) print X }' |
        # Sort the names
        sort |
        # Loop over each 
        while read PREFIX
        do
                # List everything beginning with ${PREFIX} in extended format, sorted by time
                ls -lt "${PREFIX}"*
        done | awk '/^total/ { print "" ; next } 1' # Strip off the 'total' lines


Last edited by Corona688; 11-13-2014 at 11:40 AM..
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 11-13-2014
Thanks a lot for your help.. !!!
# 6  
Old 11-13-2014
Hi.

If you use GNU ls, this might be able to help with consistent date formats:
Code:
       --time-style=STYLE
              with -l, show times using style STYLE: full-iso, long-iso, iso,
              locale, +FORMAT.  FORMAT is interpreted like ‘date'; if FORMAT
              is FORMAT1<newline>FORMAT2, FORMAT1 applies to non-recent files
              and FORMAT2 to recent files; if STYLE is prefixed with ‘posix-',
              STYLE takes effect only outside the POSIX locale
-- excerpt from man ls, q.v.

Best wishes ... cheers, drl
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use sort to sort numerical column

How to sort the following output based on lowest to highest BE? The following sort does not work. $ sort -t. -k1,1n -k2,2n bfd.txt BE31.116 0s 0s DOWN DAMP BE31.116 0s 0s DOWN DAMP BE31.117 0s 0s ... (7 Replies)
Discussion started by: sand1234
7 Replies

2. UNIX for Beginners Questions & Answers

How to copy a column of multiple files and paste into new excel file (next to column)?

I have data of an excel files as given below, file1 org1_1 1 1 2.5 100 org1_2 1 2 5.5 98 org1_3 1 3 7.2 88 file2 org2_1 1 1 2.5 100 org2_2 1 2 5.5 56 org2_3 1 3 7.2 70 I have multiple excel files as above shown. I have to copy column 1, column 4 and paste into a new excel file as... (26 Replies)
Discussion started by: dineshkumarsrk
26 Replies

3. UNIX for Dummies Questions & Answers

UNIX multiple column sort

Hi, I have a text file that has data like this chr1 156106712 156106819 LMNA 8 + 1 147 chr1 156106712 156106819 LMNA 8 + 2 147 chr1 156106712 156106819 LMNA 8 + 3 148 chr1 ... (4 Replies)
Discussion started by: mitt
4 Replies

4. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. Shell Programming and Scripting

Grep from multiple patterns multiple file multiple output

Hi, I want to grep multiple patterns from multiple files and save to multiple outputs. As of now its outputting all to the same file when I use this command. Input : 108 files to check for 390 patterns to check for. output I need to 108 files with the searched patterns. Xargs -I {} grep... (3 Replies)
Discussion started by: Diya123
3 Replies

6. Shell Programming and Scripting

Converting Single Column into Multiple rows, but with strings to specific tab column

Dear fellows, I need your help. I'm trying to write a script to convert a single column into multiple rows. But it need to recognize the beginning of the string and set it to its specific Column number. Each Line (loop) begins with digit (RANGE). At this moment it's kind of working, but it... (6 Replies)
Discussion started by: AK47
6 Replies

7. UNIX for Dummies Questions & Answers

Sort command in one column and not effect to another column

If my data is numerical : 1 = 101 2 = 102 3 = 104 4 = 104 7 = 103 8 = 103 9 = 105 I need the result like below: 1 = 101 2 = 102 3 = 103 4 = 103 7 = 104 8 = 104 9 = 105 (4 Replies)
Discussion started by: GeodusT
4 Replies

8. Shell Programming and Scripting

How to sum multiple column output with awk ?

Hi Experts, I am trying to sum multiple columns and rows with awk , I want the sum of : 1] Horizontal Sum: (rows sum): 2] Vertical Sum: (Column's sum] details: # cat file1 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 40 31 32 33 34 35 36 37 38 39 70 41 42 43 44... (2 Replies)
Discussion started by: rveri
2 Replies

9. Shell Programming and Scripting

sort on second column only based on first column

I have an input file like this... AAAlkalines Energizer AAAlkalines Energizer AAAlkalines Energizer AAAlkalines Sunlight AAAlkalines Sunlight AAAlkalines Sunlight AAAlkalines Energizer AAAlkalines Energizer AAAlkalines Energizer AAASalines ... (7 Replies)
Discussion started by: malcomex999
7 Replies

10. Shell Programming and Scripting

Question about sort specific column and print other column at the same time !

Hi, This is my input file: ali 5 usa abc abu 4 uk bca alan 6 brazil bac pinky 10 utah sdc My desired output: pinky 10 utah sdc alan 6 brazil bac ali 5 usa abc abu 4 uk bca Based on the column two, I want to do the descending order and print out other related column at the... (3 Replies)
Discussion started by: patrick87
3 Replies
Login or Register to Ask a Question