Sponsored Content
Homework and Emergencies Emergency UNIX and Linux Support Sorting file content by file extensions Post 302639495 by otheus on Saturday 12th of May 2012 12:24:57 AM
Old 05-12-2012
I thought of two better ways to do this.

First, you can use the traditional sort, and this will work fine for 99% of the cases:
Code:
ls -1 | sort -t. -k 3,3 -k 2,2 -k 1,1

You tell sort to order by the 3rd extension, then the 2nd, then the 1st... and sort ignores non-existent fields. The only problem with this sort method is that you get this kind of weird ordering:

Code:
bar
foo
bar.zip
foo.zip
foo.bar.jpg
foo.bar.zip
bar.foo.zip

That is, fields with more than 1 extension have higher sorting precedence than fields with two. So the two zip files seem out-of-place.


So for the best ordering -- the one most likely to be expected, you make the last extension "special" by inserting a special character before the final period. Then sort, then remove the special character. You can use path-separators because those are never part of the filename.
Code:
ls -1 | sed 's/\(\.[^.]*\)$/\/\1/' | sort -t/ -k 2,2 -k 1,1  |  sed 's/\/\([^/]*\)$/\1/'

I'll admit: That's ugly for the command line. It could be a bit nicer if you don't need to worry about full path-names in your list.


Postscript: On Linux, you can find the "rev" command with the util-linux suite. It prints out each line in the file in reverse, so you can use drl's technique in that environment:

Code:
ls -1 | rev | sort | rev

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File name extensions

Hello people, I was wondering if anyone could help me? I want to produce a shell script that changes the filename extension on all matching file. E.G. change all files called ‘something.rtf' to ‘something.doc' by giving the command: Changex rtf doc *where ‘Changex' is the name of... (2 Replies)
Discussion started by: thurrock
2 Replies

2. UNIX for Dummies Questions & Answers

sorting file content on columns

guys i have a question: i'd like to sort files (as many I want) in columns so to visualize them one near the other...so let's say i have just 2 files: FILE1 John Mary Bridget FILE2 Anne Robert Mark i would like to obtain: John Anne Mary Robert Bridget ... (2 Replies)
Discussion started by: marshmallow
2 Replies

3. Shell Programming and Scripting

Sorting Files according to their Extensions...

I am trying to write a Korne Shell Script wherein we have to sort files according to their extensions(for eg. 1.sh, 5.sh, 9.sh together; 4.csh, 120.csh, 6.csh together and 7.ksh, 2.ksh, 59.ksh together) and move them to their respective directories viz. sh, csh and ksh... I think,... (1 Reply)
Discussion started by: marconi
1 Replies

4. Shell Programming and Scripting

Checking file extensions

I am trying to store file with certain file extensions to list but having some problems. Here is a part of the code set fryLst = "" set fxtLst = "" foreach f ($AfullNameLst) set fname = $f:r set fext = $f:e if ("$fext" == ".ry") set fryLst = "$fryLst $f" if ("$fext" == ".xt")... (2 Replies)
Discussion started by: kristinu
2 Replies

5. Shell Programming and Scripting

Sorting content of file

hi ladies and gents: can you give me a command to sort content of file and save it to the file itself: file1 roy@emerson.com joy@emerson.com irish@emerson.com output would be file1 on same directory: file1: irish@emerson.com joy@emerson.com roy@emerson.com (6 Replies)
Discussion started by: linuxgeek
6 Replies

6. Shell Programming and Scripting

Remove comments from file with specific file name extensions

Hello Unix board community, I have to program a shell script, but I am a complete noob so I hope I get some help here. The assignment is as follows: The program removes all comments regardless of formatting or language from files with specific file name extensions (php, css, js, ...).... (3 Replies)
Discussion started by: TheZeusMan
3 Replies

7. Shell Programming and Scripting

Sed: replace content from file with the content from file

Hi, I am having trouble while using 'sed' with reading files. Please help. I have 3 files. File A, file B and file C. I want to find content of file B in file A and replace it by content in file C. Thanks a lot!! Here is a sample of my question. e.g. (file A: a.txt; file B: b.txt; file... (3 Replies)
Discussion started by: dirkaulo
3 Replies

8. UNIX for Dummies Questions & Answers

Need to exclude certain file extensions while listing the file using ls

Hi friends, I need to check for the latest file say i have list of files like this test_files test_files.1 test_files.2 test_files.3.bin.Z I do it this way ls -lrt test_files*|tail -1 Now i need to exclude test_files.3.bin.Z even if it is the latest file,how do i do... (3 Replies)
Discussion started by: 100bees
3 Replies

9. Shell Programming and Scripting

How to remove exisiting file content from a file and have to append new file content?

hi all, i had the below script x=`cat input.txt |wc -1` awk 'NR>1 && NR<'$x' ' input.txt > output.txt by using above script i am able to remove the head and tail part from the input file and able to append the output to the output.txt but if i run it for second time the output is... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

10. Shell Programming and Scripting

List the files after sorting based on file content

Hi, I have two pipe separated files as below: head -3 file1.txt "HD"|"Nov 11 2016 4:08AM"|"0000000018" "DT"|"240350264"|"56432" "DT"|"240350264"|"56432" head -3 file2.txt "HD"|"Nov 15 2016 2:18AM"|"0000000019" "DT"|"240350264"|"56432" "DT"|"240350264"|"56432" I want to list the... (6 Replies)
Discussion started by: Prasannag87
6 Replies
Debian::Dependencies(3pm)				User Contributed Perl Documentation				 Debian::Dependencies(3pm)

NAME
Debian::Dependencies - a list of Debian::Dependency objects SYNOPSIS
my $dl = Debian::Dependencies->new('perl, libfoo-perl (>= 3.4)'); print $dl->[1]->ver; # 3.4 print $dl->[1]; # libfoo-perl (>= 3.4) print $dl; # perl, libfoo-perl (>= 3.4) $dl += 'libbar-perl'; print $dl; # perl, libfoo-perl (>= 3.4), libbar-perl print Debian::Dependencies->new('perl') + 'libfoo-bar-perl'; # simple 'sum' print Debian::Dependencies->new('perl') + Debian::Dependencies->new('libfoo, libbar'); # add (concatenate) two lists print Debian::Dependencies->new('perl') + Debian::Dependency->new('foo'); # add depeendency to a list DESCRIPTION
Debian::Dependencies a list of Debian::Dependency objects, with automatic construction and stringification. Objects of this class are blessed array references. You can safely treat them as arrayrefs, as long as the elements you put in them are instances of the Debian::Dependency class. When used in string context, Debian::Dependencies converts itself into a comma-delimitted list of dependencies, suitable for dependency fields of debian/control files. CLASS METHODS new(dependency-string) Constructs a new Debian::Dependencies object. Accepts one scalar argument, which is parsed and turned into an arrayref of Debian::Dependency objects. Each dependency should be delimitted by a comma and optional space. The exact regular expression is "/s*,s*/". OBJECT METHODS add( dependency[, ... ] ) Adds dependency (or a list of) to the list of dependencies. If the new dependency is a subset of or overlaps some of the old dependencies, it is not duplicated. my $d = Debian::Dependencies('foo, bar (<=4)'); $d->add('foo (>= 4), bar'); print "$d"; # foo (>= 4), bar (>= 4) dependency can be either a Debian::Dependency object, a Debian::Deendencies object, or a string (in which case it is converted to an instance of the Debian::Dependencies class). remove( dependency, ... ) =item remove( dependencies, ... ) Removes a dependency from the list of dependencies. Instances of Debian::Dependency and Debian::Dependencies classes are supported as arguments. Any non-reference arguments are coerced to instances of Debian::Dependencies class. Only dependencies that are subset of the given dependencies are removed: my $deps = Debian::Dependencies->new('foo (>= 1.2), bar'); $deps->remove('foo, bar (>= 2.0)'); print $deps; # bar Returns the list of the dependencies removed. has( dep ) Return true if the dependency list contains given dependency. In other words, this returns true if the list of dependencies guarantees that the given dependency will be satisfied. For example, "foo, bar" satisfies "foo", but not "foo (>= 5)". prune() This method is deprecated. If you want to sort the dependency list, either call "sort" or use normal perl sorting stuff on the dereferenced array. sort() Sorts the dependency list by packagee name, version and relation. SEE ALSO
Debian::Dependency AUTHOR
Damyan Ivanov <dmn@debian.org> COPYRIGHT &; LICENSE Copyright (C) 2008, 2009, 2010 Damyan Ivanov <dmn@debian.org> Copyright (C) 2009 gregor herrmann <gregoa@debian.org> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. perl v5.14.2 2012-01-15 Debian::Dependencies(3pm)
All times are GMT -4. The time now is 07:16 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy