How to sort the timestamp in the filename in shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to sort the timestamp in the filename in shell script?
# 1  
Old 03-04-2014
How to sort the timestamp in the filename in shell script?

originally the shellscript


Code:
#ln_file_name=`echo $ld_interface_date"_"${8}".csv"`
#ln_file_name=`echo 201202011527_HL_HLTM1_B04A.csv`
ln_file_name="*"`echo ${7}".csv"`
get_file_list_1=$log_path"tm1_file_list.gfl1"

        cd ${source_path}
        echo "Try to find any file exist in the source_path"
        ls $ln_file_name > $get_file_list_1 
         # Count the number of files exist in source_path
     if ( test -s $get_file_list_1 )


how do i change this code "ls $ln_file_name > $get_file_list_1 "to sort the
timestamp in fielname?

the fielname format "alex_li_20140304102033_TM1" 20140304102033 is the timestamp

for example
alex_li_20140304102033_TM1.csv

BB_li_20140304112033_TM1.csv

BX_li_20140304082043_TM1.csv

BC_li_20140304202033_TM1.csv

after the sort it will be the oldest file list first

BX_li_20140304082043_TM1.csv

alex_li_20140304102033_TM1.csv

BB_li_20140304112033_TM1.csv

BC_li_20140304202033_TM1.csv
# 2  
Old 03-04-2014
The ls command will sort the files only if the timestamp of those files is the same as that embedded in the filename otherwise you'd have to use awk or perl for sorting on that specific part of the filename...
# 3  
Old 03-04-2014
Quote:
Originally Posted by shamrock
The ls command will sort the files only if the timestamp of those files is the same as that embedded in the filename otherwise you'd have to use awk or perl for sorting on that specific part of the filename...
ya i am trying to sort the specific part of the filename, for the last modified date timestamp i am able to sort this


i tried ls -t | awk -F '_' '{print $3}'

it will output as 20140304202022

how do i output as alex_li_20140304202022_TM1.csv to remain format for

"ls $ln_file_name > $get_file_list_1 ", otherwise , only output timestamp will make my program fail lol

and still able to be sorted?

thanks Smilie
# 4  
Old 03-04-2014
Try this awk script...
Code:
ls -1t *.csv | awk -F"_" '{
    x[NR] = $3
    y[$3] = $0
} END {
    for (i=1; i<NR; i++)
        for (j=1; j<(NR+1-i); j++)
            if (x[j] > x[j+1]) {t = x[j]; x[j] = x[j+1]; x[j+1] = t}
    for (i=1; i<=NR; i++) print y[x[i]]
}'

# 5  
Old 03-04-2014
Try this
Code:
ls *.csv | sort -t_ -k3,3n

These 2 Users Gave Thanks to Chubler_XL For This Post:
# 6  
Old 03-04-2014
Quote:
Originally Posted by Chubler_XL
Try this
Code:
ls *.csv | sort -t_ -k3,3n

Gosh what was i thinking Smilie as yours Chubler_XL is the best...
# 7  
Old 03-04-2014
Quote:
Originally Posted by Chubler_XL
Try this
Code:
ls *.csv | sort -t_ -k3,3n

thanks for your answer, it work, but i discover that

the format is a bit different for users,

alex_li_20140304121212_tm1.csv

alex_cf_li_20140304121212_tm1.csv

there might be some user with extra underscore.

so counting from suffix is better, the time stamp si always before _tm1.csv, how do i change ls *.csv | sort -t_ -k3,3n to count the segment before _tm1.csv ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can i make my cron/script to generate a log filename with timestamp in it ?

Hello Friends, I would like my script to display date timestamps in the file name for every script execution. Below is the scenario: (just for testing purpose) I scheduled a cron job, lets say it runs every 5 min and record/logs output in to a log file. 0,5,10,15,20,25,30,35,40,45,50,55 *... (5 Replies)
Discussion started by: System Admin 77
5 Replies

2. Shell Programming and Scripting

Filename timestamp

Hi Gurus, I have different files with different timestamp and different base file name, I have to group those files based on basename and provide a unique file name for similar file names. My Directory has following files. abc_filename_20130623:00:09:00.txt... (1 Reply)
Discussion started by: user_linux
1 Replies

3. Shell Programming and Scripting

Bash script to sort files into folder according to a string in the filename

Hi all. I am very new to linux scripting and i have a task i can only solve with a script. I need to sort files base on the date string in their filenames and create a folder using the same date string then move the files to their respective folders. Scenario: Folder Path:... (1 Reply)
Discussion started by: ace47
1 Replies

4. Shell Programming and Scripting

URGENT!!! bash script to sort files into folder according to a string in the filename

Hi all. I am very new to linux scripting and i have a task i can only solve with a script. I need to sort files base on the date string in their filenames and create a folder using the same date string then move the files to their respective folders. Scenario: Folder Path:... (1 Reply)
Discussion started by: ace47
1 Replies

5. Shell Programming and Scripting

Get filename with size and timestamp

Hi, Below is a directory containing links new2,list,new1. I need to get the size and timestamp for them. How do i get these details. Please help lrwxrwxrwx 1 xxx abc 11 Nov 24 17:34 new2 -> ./org1/new2 lrwxrwxrwx 1 xxx abc 11 Nov 24 17:34 list -> ./org2/list lrwxrwxrwx 1 xxx abc 10... (2 Replies)
Discussion started by: pradebban
2 Replies

6. UNIX for Dummies Questions & Answers

Shell script which will sort all the files in a directory with the timestamp they were created

Team, Pls help writing a shell script which will sort all the files in a directory with the timestamp they were created. (like ls -lrt) (6 Replies)
Discussion started by: asappidi
6 Replies

7. Shell Programming and Scripting

How to extract timestamp from the filename?

Hi., My file name is of the format: name_abc_20100531_142528.txt where., my timestamp is of the format: yyyymmdd_hhmmss How to extract the date strring and time string into seperate variables in the shell script, after reading the file as the input? I want to get the variables... (9 Replies)
Discussion started by: av_vinay
9 Replies

8. Shell Programming and Scripting

Timestamp in the filename

Hi i want to replace the previous time stamp with the current timsatp at the start of the file like 20090710_113354_FT0710a.txt this one to 20091111__113354_FT0710a.txt thanks in advance (3 Replies)
Discussion started by: Reddy482
3 Replies

9. Shell Programming and Scripting

add timestamp to filename

Does anyone know how to add a timestamp to a file's name extension in a shell script? Please help.. (3 Replies)
Discussion started by: walterja
3 Replies

10. UNIX for Dummies Questions & Answers

how to add a timestamp to a filename?

whats going on guys. below is a script i made and am just curious if there is a "time stamp" command. so i can set the timestamp in a filename. #! /bin/ksh # # This scripts takes a list of files in the INDIR variable and compairs it to a list of files that are open in the same directory.... (2 Replies)
Discussion started by: Optimus_P
2 Replies
Login or Register to Ask a Question