LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-10-2014, 12:03 AM   #1
kumar554
LQ Newbie
 
Registered: Jul 2014
Posts: 4
Blog Entries: 1

Rep: Reputation: Disabled
Shell script for sorting files with same extension(.xml,.dat etc..)in a directory in


Hi,

Pls help me in writing a shell script for sorting the below files w.r.t latest date.
here date is within the file name, not according to the system date.

for example given files:
format is XXXXXX_YYYYMMDDHHMMSS-RequestID.xml


abcdef_20140713114020-abc123.xml
tuvwxy_20140716134520-abc123.xml
hijklm_20140714123040-abc123.xml
nopqrs_20140712104010-abc123.xml


Sorted order is :

nopqrs_20140712104010-abc123.xml
abcdef_20140713114020-abc123.xml
hijklm_20140714123040-abc123.xml
tuvwxy_20140716134520-abc123.xml

Thanks,
Santosh

Last edited by kumar554; 07-16-2014 at 02:12 AM.
 
Old 07-10-2014, 12:13 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,
Quote:
Originally Posted by rsnkumar View Post
pls help me writing a shell script for sorting files with same extension(.xml,.dat etc..)in a directory in a sequence based on the timestamp, yyyyMMddhh[24hr]mmss.
You need to learn how to use ls with the -t flag. You also need learn about shell globing.
Quote:
Originally Posted by rsnkumar View Post
I have zero knowledge on shell scripting..
Time to learn then.

Quote:
Originally Posted by rsnkumar View Post
Pls provide the whole structure of the script, not only the logic..
No. I will provide a hint though.
Code:
ls -t -1 *.xml
Quote:
Originally Posted by rsnkumar View Post
will be very gr8 ful.
This is not sms, please don't treat it as such.

Evo2.
 
Old 07-10-2014, 12:25 AM   #3
kumar554
LQ Newbie
 
Registered: Jul 2014
Posts: 4

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Smile

Hi Evo,

Thanks for the quick response..Pls help me how to incorporate the below files which are to be sorted.pls share any template for better understanding.

Files to be sorted

abcdeg_20070809141159.xml
abcdeg_20070809141157.xml
abcdeg_20070809141155.xml
abcdeg_20070809141151.xml
abcdeg_20070809141149.xml
abcdeg_20070809141148.xml


Then The Sorted Order is

abcdeg_20070809141148
abcdeg_20070809141149
abcdeg_20070809141151
abcdeg_20070809141155
abcdeg_20070809141157
abcdeg_20070809141159

Thanks,
Santosh
 
Old 07-10-2014, 12:36 AM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

So you want to sort them my name.. that is the default sort order for both a shell glob and ls. Eg
Code:
ls -1
However I'm beginning to think that I really don't understand what you are trying to do. "Sorting files"? In what context? I think you need to give the bigger picture about what you are actually trying to achieve.

Evo2.
 
Old 07-10-2014, 12:59 AM   #5
kumar554
LQ Newbie
 
Registered: Jul 2014
Posts: 4

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Hi,

consider the files are like below:

Format: yyyyMMdd_hhmmss-filename.xml

20140710_111936-xyz.xml
20140708_111740-abc.xml
20140705_112550-def.xml

Sorting order should be w.r.t latest date first.The order should be:

20140708_111740-abc.xml
20140705_112550-def.xml
20140710_111936-xyz.xml(latest date should appear last)

Now, I need to check the date and time stamp as available in the file names (and not the unix system date timestamp), compare it, and pick up the file with the lowest date timestamp in its name, first and latest date at last.

Hope you got my point.

Thanks,
Santosh
 
Old 07-10-2014, 01:28 AM   #6
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,
Quote:
Sorting order should be w.r.t latest date first.The order should be:

20140708_111740-abc.xml
20140705_112550-def.xml
20140710_111936-xyz.xml(latest date should appear last)
That is *EXACTLY* the output you would get from
Code:
ls -1
or
Code:
echo * | tr ' ' '\n'

Quote:
Now, I need to check the date and time stamp as available in the file names (and not the unix system date timestamp), compare it,
compare it to what?
Quote:
and pick up the file with the lowest date timestamp in its name, first and latest date at last.
Code:
first=$(echo * | tr ' ' '\n' | head -1)
last=$(echo * | tr ' ' '\n' | tail -1)

echo "The first file is ${first}"
echo "The last file is ${last}"

I have to admit I'm pretty lost. I really don't know what you are trying to do. Again, giving the bigger picture would help. I suspect if you learn just a little bit of how to use the command line you'd be trivially able to do what you want, but since I don't understand what you are trying to do I don't think I can help.

Evo2.
 
Old 07-10-2014, 07:40 AM   #7
costa may
LQ Newbie
 
Registered: Jun 2014
Posts: 8

Rep: Reputation: Disabled
Thumbs up

ls -ls *.xml I think it can help you
 
Old 07-16-2014, 02:16 AM   #8
kumar554
LQ Newbie
 
Registered: Jul 2014
Posts: 4

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Sorting files wrt date within filename

Hi,

Pls help me in writing a shell script for sorting the below files w.r.t latest date.
here date is within the file name, not according to the system date.

for example given files:
format is XXXXXX_YYYYMMDDHHMMSS-RequestID.xml


abcdef_20140713114020-abc123.xml
tuvwxy_20140716134520-abc123.xml
hijklm_20140714123040-abc123.xml
nopqrs_20140712104010-abc123.xml


Sorted order is :

nopqrs_20140712104010-abc123.xml
abcdef_20140713114020-abc123.xml
hijklm_20140714123040-abc123.xml
tuvwxy_20140716134520-abc123.xml

Thanks,
Santosh
 
Old 07-16-2014, 03:44 AM   #9
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

ok, so the files are not as you described them in your earlier posts. You can use sort for this. Eg sorting on everything after the underscore:
Code:
ls -1 | sort --field-separator=_ --key=2
Evo2.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Updating XML files using a shell script. Cpare Programming 3 04-01-2011 05:16 PM
[SOLVED] shell script to search for files of specific extension and delete them all rhklinux Linux - Newbie 12 07-12-2010 12:14 AM
script for deleting .dat files which > 5 mb from a directory and its subdirectory anindyabhattacharjee Linux - Enterprise 2 01-15-2007 11:38 PM
Bash script: symbolic linking all files with same extension in a directory Katachi Programming 1 12-25-2006 09:42 AM
How program to read files with extension .dat y .cfg files COMTRADE in fedora 1? ivonne Linux - Software 0 11-22-2004 11:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration