The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sorting Files by date and moving files in date order rebel64 Shell Programming and Scripting 2 03-11-2008 12:45 PM
Sorting by date and time padmundo UNIX for Dummies Questions & Answers 7 07-10-2007 05:04 AM
Sorting a date coulumn rinku11 Shell Programming and Scripting 2 05-15-2007 07:51 PM
date sorting nmilella UNIX for Dummies Questions & Answers 3 07-11-2006 04:11 AM
sorting on date Duckman UNIX for Dummies Questions & Answers 6 03-29-2001 04:32 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-11-2007
LiquidChild LiquidChild is offline
Registered User
  
 

Join Date: Jul 2005
Location: Belfast
Posts: 49
Date Sorting

Hi,

I have a list of files that take on the format ABCDE_yymmdd and wish to sort them in ascending date order. I can't use the unix time stamp for the file as this could possibly be different from the date given in the file name.

Does anyone know of any way this can be done using unix shell scripting and/or awk(nawk)?

What i already have:

constructFileList()
{

if [ -f $DataDir/$FileListFile ]
then
rm -f $DataDir/$FileListFile
fi

for FILE in `ls -1 $DataDir/ABCDE_??????.dat`
do
echo $(basename $FILE)|nawk -f sort.awk
done |sort|while read FILENAME
do
echo ${FILENAME#??????} >> $DataDir/$FileListFile
done
}

The sort.awk script:

function prisort(STRING)
{
return_str = substr(STRING,11,2) substr(STRING,9,2) substr(STRING,7,2) STRING
return return_str
}

#Main Routine
{
print prisort($1)
}

Thanks

Last edited by LiquidChild; 04-11-2007 at 04:38 AM..
  #2 (permalink)  
Old 04-11-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
ls PRIML_* | sort -t"_" -k2
  #3 (permalink)  
Old 04-11-2007
LiquidChild LiquidChild is offline
Registered User
  
 

Join Date: Jul 2005
Location: Belfast
Posts: 49
Wow, impressive, so much easier than what i was doing thanks anbu23, can you just give a brief explanation of what the -t "_" -k2 does, i would like to understand what I am using so I can build my knowledge.

Edit: I think i get what the -t option is for, to spilt the string to be able to sort on just the date part?

Thanks.
  #4 (permalink)  
Old 04-11-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
LiquidChild,
For the "sort" command:
-t --> specifies the field separation.
-k --> specifies the field sequence.

If you type: man sort
you can see a full explanation of the "sort" command in your system.
  #5 (permalink)  
Old 04-11-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Quote:
Originally Posted by LiquidChild
Wow, impressive, so much easier than what i was doing thanks anbu23, can you just give a brief explanation of what the -t "_" -k2 does, i would like to understand what I am using so I can build my knowledge.

Edit: I think i get what the -t option is for, to spilt the string to be able to sort on just the date part?

Thanks.
-t char
Use the single character char as the default field separator, instead of the default of whitespace.

-k
Define the sort key field.
  #6 (permalink)  
Old 05-25-2007
LiquidChild LiquidChild is offline
Registered User
  
 

Join Date: Jul 2005
Location: Belfast
Posts: 49
This does not seem to work in the following though

FILE_230580.ok
FILE_230590.ok
FILE_010107.ok

As the ordering is then:

FILE_010107.ok
FILE_230580.ok
FILE_230590.ok
  #7 (permalink)  
Old 05-25-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Quote:
Originally Posted by LiquidChild
This does not seem to work in the following though

FILE_230580.ok
FILE_230590.ok
FILE_010107.ok

As the ordering is then:

FILE_010107.ok
FILE_230580.ok
FILE_230590.ok
Code:
$ ls FILE_*
FILE_010107.ok  FILE_230580.ok  FILE_230590.ok
$ ls FILE_* | sort -t"_" -k2
FILE_010107.ok
FILE_230580.ok
FILE_230590.ok
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0