LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell script for sorting files with same extension(.xml,.dat etc..)in a directory in (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-for-sorting-files-with-same-extension-xml-dat-etc-in-a-directory-in-4175510655/)

kumar554 07-10-2014 12:03 AM

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

evo2 07-10-2014 12:13 AM

Hi,
Quote:

Originally Posted by rsnkumar (Post 5201512)
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 (Post 5201512)
I have zero knowledge on shell scripting..

Time to learn then.

Quote:

Originally Posted by rsnkumar (Post 5201512)
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 (Post 5201512)
will be very gr8 ful.

This is not sms, please don't treat it as such.

Evo2.

kumar554 07-10-2014 12:25 AM

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

evo2 07-10-2014 12:36 AM

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.

kumar554 07-10-2014 12:59 AM

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

evo2 07-10-2014 01:28 AM

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.

costa may 07-10-2014 07:40 AM

ls -ls *.xml I think it can help you

kumar554 07-16-2014 02:16 AM

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

evo2 07-16-2014 03:44 AM

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.


All times are GMT -5. The time now is 07:43 PM.