Sorting based on File name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting based on File name
# 1  
Old 01-27-2017
Sorting based on File name

Hi All

I have a requirement to list all the files in chronological order based on the date value in the file name.For ex if I have three files as given below

Code:
ABC_TEST_20160103_1012.txt
ABC_TEST_20160229_1112.txt
ABC_TEST_20160229_1112.txt

I have written code as given below to list out the files.This is the command which I am trying to execute from Data stage job which is running on UNIX OS.

Code:
ls -m ABC*|sort -t_ -k3

The above code is working in this scenario.But in case if we receive a file name as below, this will not work, I need to change the command accordingly,as date is coming as first portion of the file name.

Code:
20160122_2130_ABC_TEST.txt

I want to write a common command in such a way that it will sort automatically based on the date value in the file name, irrespective of the position where the date value is coming in the file
# 2  
Old 01-27-2017
Try something like:
Code:
ls | sed 's/.*\([0-9]\{8\}\)/\1 &/' | sort -k1,1n | cut -c10-

This assumes that 8 digits always constitute a date..


--
If the date is always followed by an underscore you could make this a bit more precise like so:
Code:
ls | sed 's/.*\([0-9]\{8\}\)_/\1 &/' | sort -k1,1n | cut -c10-


Last edited by Scrutinizer; 01-27-2017 at 03:51 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 01-28-2017
Quote:
Originally Posted by Scrutinizer
Try something like:
Code:
ls | sed 's/.*\([0-9]\{8\}\)/\1 &/' | sort -k1,1n | cut -c10-

This assumes that 8 digits always constitute a date..


--
If the date is always followed by an underscore you could make this a bit more precise like so:
Code:
ls | sed 's/.*\([0-9]\{8\}\)_/\1 &/' | sort -k1,1n | cut -c10-

And if the filenames always contain an 8 digit date followed by an underscore and a 4 digit time (YYYYMMDD_hhmm) and the desire is to sort on the date and time, you could try:
Code:
ls | sed 's/.*\([0-9]\{8\}_[0-9]\{4\}\)/\1 &/' | sort -k1,1 | cut -c14-

Note that I removed the sort -n option because an alphanumeric sort will get the desired results on the 1st pass while a numeric sort would stop comparing at the underscore (ignoring the timestamp on the 1st pass).
These 2 Users Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting/Arranging file based on tags using awk

Hi, I have file which contains data based on tags. Output of the file should be in order of tags. Below are the files : Tags.txt f12 f13 f23 f45 f56 Original data is like this : Data.txt 2017/01/04|09:07:00:021|R|XYZ|38|9|1234|f12=CAT|f23=APPLE|f45=PENCIL|f13=CAR... (5 Replies)
Discussion started by: Prathmesh
5 Replies

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

3. Shell Programming and Scripting

Sorting file based on a numeric column

Hi, I use UBUNTU 12.04. I have a file with this structure: Name 2 1245787 A G 12 14 12 14 .... Name 1 1245789 C T 13 12 12 12..... I would like to sort my file based on the second column so to have this output for example: Name 1 1245789 C T 13 12 12 12..... Name 2 1245787 A G 12 14... (4 Replies)
Discussion started by: Homa
4 Replies

4. Shell Programming and Scripting

Sorting file based on name

Hi team, We have few files landing to our server based on sequence number. These files have to be processed in the sequence number order. Once the sequence number has reached its maximum, the files with sequence number 0000 has to be processed. For example: IN9997 IN9998 IN9999 IN0000... (7 Replies)
Discussion started by: anijan
7 Replies

5. UNIX for Dummies Questions & Answers

Sorting a file based on the absolute value of a column

I would like to sort a tab delimited text file based on the absolute value of its second column. How do I go about doing that? Thanks! Example input: A -12 B 0 C -6 D 7 Output: A -12 D 7 C -6 B 0 (4 Replies)
Discussion started by: evelibertine
4 Replies

6. Shell Programming and Scripting

sorting based on a specified column in a text file

I have a tab delimited file with 5 columns 79 A B 20.2340 6.1488 8.5086 1.3838 87 A B 0.1310 0.0382 0.0054 0.1413 88 A B 46.1651 99.0000 21.8107 0.2203 89 A B 0.1400 0.1132 0.0151 0.1334 114 A B 0.1088 0.0522 0.0057 0.1083 115 A B... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

7. Shell Programming and Scripting

Sorting file based on names

Hi I have some files in directory and the names of files are like jnhld_15233_2010-11-23 jnhld_15233_2007-10-01 jnhld_15233_2001-05-04 jnhld_15233_2011-11-11 jnhld_15233_2005-06-07 jnhld_15233_2000-04-01 ..etc How can i sort these files based on the date in the file name so that ... (4 Replies)
Discussion started by: morbid_angel
4 Replies

8. UNIX for Dummies Questions & Answers

Sorting data in file based on field in another file

Hi, I have two files, one of which I would like to sort based on the order of the data in the second. I would like to do this using a simple unix statement. My two files as follows: File 1: 12345 1 2 2 2 0 0 12349 0 0 2 2 1 2 12350 1 2 1 2 2 2 . . . File2: 12350... (3 Replies)
Discussion started by: kasan0
3 Replies

9. Shell Programming and Scripting

sorting csv file based on column selected

Hi all, in my csv file it'll look like this, and of course it may have more columns US to UK;abc-hq-jcl;multimedia UK to CN;def-ny-jkl;standard DE to DM;abc-ab-klm;critical FD to YM;la-yr-tym;standard HY to MC;la-yr-ytm;multimedia GT to KJ;def-ny-jrt;critical I would like to group... (4 Replies)
Discussion started by: tententen
4 Replies

10. Shell Programming and Scripting

sorting file based on two or more columns

Hi gang. I'm using a unix/mac system and i'm trying to sort a file (more than 1,000,000 lines). chr1 100000965 100001001 - chr1 100002155 100002191 + chr1 100002165 100002201 + chr1 100002525 100002561 - chr1 10000364 ... (2 Replies)
Discussion started by: labrazil
2 Replies
Login or Register to Ask a Question