Solved

Shell script to select newest file name from their group

Posted on 2012-04-05
4
543 Views
Last Modified: 2012-04-06
Hello Experts,

1. I have file "MSTEST.txt" containing a lot of number.
2. I use command: cat MSTEST.txt | awk '{print "ls -lrt | grep "$1}' | sh <== to get list of files, please see in "MYLIST.txt"

3. In "MYLIST.txt" look like:
-rw-r--r--   1 isladm   mtnu      325126 Apr  4 11:30 1333528214563_SUBREGREQ_0312102619_906D1F73BCA546748C82CBF7BAF62834.dat
-rw-r--r--   1 isladm   mtnu      324718 Apr  4 12:44 1333532684474_SUBREGREQ_0312513701_F5725EEBA9434C30AFD71BFD6DE5E1EA.dat
-rw-r--r--   1 isladm   mtnu      349820 Apr  4 09:27 1333520876474_SUBREGREQ_0312516780_6615A8FC810B45E5AB75C41B63B8D95F.dat
-rw-r--r--   1 isladm   mtnu      349820 Apr  4 09:42 1333521775659_SUBREGREQ_0312516780_6615A8FC810B45E5AB75C41B63B8D95F.dat
-rw-r--r--   1 isladm   mtnu      349820 Apr  4 09:57 1333522677642_SUBREGREQ_0312516780_6615A8FC810B45E5AB75C41B63B8D95F.dat
-rw-r--r--   1 isladm   mtnu      349820 Apr  4 11:42 1333528976543_SUBREGREQ_0312516780_6615A8FC810B45E5AB75C41B63B8D95F.dat
-rw-r--r--   1 isladm   mtnu      349820 Apr  4 12:57 1333533479959_SUBREGREQ_0312516780_6615A8FC810B45E5AB75C41B63B8D95F.dat
-rw-r--r--   1 isladm   mtnu      349820 Apr  4 13:27 1333535278784_SUBREGREQ_0312516780_6615A8FC810B45E5AB75C41B63B8D95F.dat
-rw-r--r--   1 isladm   mtnu      349820 Apr  4 14:12 1333537979432_SUBREGREQ_0312516780_6615A8FC810B45E5AB75C41B63B8D95F.dat
-rw-r--r--   1 isladm   mtnu      349820 Apr  4 15:58 1333544280178_SUBREGREQ_0312516780_6615A8FC810B45E5AB75C41B63B8D95F.dat
-rw-r--r--   1 isladm   mtnu      349820 Apr  4 16:27 1333546078052_SUBREGREQ_0312516780_6615A8FC810B45E5AB75C41B63B8D95F.dat
-rw-r--r--   1 isladm   mtnu      349820 Apr  4 17:27 1333549678331_SUBREGREQ_0312516780_6615A8FC810B45E5AB75C41B63B8D95F.dat
-rw-r--r--   1 isladm   mtnu      349820 Apr  4 17:42 1333550576357_SUBREGREQ_0312516780_6615A8FC810B45E5AB75C41B63B8D95F.dat
-rw-r--r--   1 isladm   mtnu      325395 Apr  4 16:00 1333544422270_SUBREGREQ_0312517226_B6AA43723EFA4123897B793A1F9DC282.dat
-rw-r--r--   1 isladm   mtnu      325395 Apr  4 17:02 1333548137093_SUBREGREQ_0312517226_B6AA43723EFA4123897B793A1F9DC282.dat
-rw-r--r--   1 isladm   mtnu      319949 Apr  4 09:03 1333519391079_SUBREGREQ_0392277106_7C2B9915ECEE4A6A8134471E1C4F515C.dat
-rw-r--r--   1 isladm   mtnu      319949 Apr  4 09:18 1333520293690_SUBREGREQ_0392277106_7C2B9915ECEE4A6A8134471E1C4F515C.dat
-rw-r--r--   1 isladm   mtnu      319949 Apr  4 09:52 1333522325468_SUBREGREQ_0392277106_7C2B9915ECEE4A6A8134471E1C4F515C.dat
-rw-r--r--   1 isladm   mtnu      319949 Apr  4 10:19 1333523992716_SUBREGREQ_0392277106_7C2B9915ECEE4A6A8134471E1C4F515C.dat
-rw-r--r--   1 isladm   mtnu      319949 Apr  4 10:48 1333525697445_SUBREGREQ_0392277106_7C2B9915ECEE4A6A8134471E1C4F515C.dat
-rw-r--r--   1 isladm   mtnu      319949 Apr  4 11:18 1333527503227_SUBREGREQ_0392277106_7C2B9915ECEE4A6A8134471E1C4F515C.dat
-rw-r--r--   1 isladm   mtnu      319949 Apr  4 11:33 1333528392396_SUBREGREQ_0392277106_7C2B9915ECEE4A6A8134471E1C4F515C.dat
-rw-r--r--   1 isladm   mtnu      319949 Apr  4 12:03 1333530205140_SUBREGREQ_0392277106_7C2B9915ECEE4A6A8134471E1C4F515C.dat
-rw-r--r--   1 isladm   mtnu      319949 Apr  4 13:03 1333533786384_SUBREGREQ_0392277106_7C2B9915ECEE4A6A8134471E1C4F515C.dat
-rw-r--r--   1 isladm   mtnu      319949 Apr  4 13:18 1333534689871_SUBREGREQ_0392277106_7C2B9915ECEE4A6A8134471E1C4F515C.dat
-rw-r--r--   1 isladm   mtnu      319949 Apr  4 13:33 1333535588594_SUBREGREQ_0392277106_7C2B9915ECEE4A6A8134471E1C4F515C.dat
-rw-r--r--   1 isladm   mtnu      319949 Apr  4 13:48 1333536487219_SUBREGREQ_0392277106_7C2B9915ECEE4A6A8134471E1C4F515C.dat
-rw-r--r--   1 isladm   mtnu      319949 Apr  4 14:03 1333537392210_SUBREGREQ_0392277106_7C2B9915ECEE4A6A8134471E1C4F515C.dat
-rw-r--r--   1 isladm   mtnu      319949 Apr  4 14:18 1333538294354_SUBREGREQ_0392277106_7C2B9915ECEE4A6A8134471E1C4F515C.dat
-rw-r--r--   1 isladm   mtnu      319949 Apr  4 14:48 1333540123302_SUBREGREQ_0392277106_7C2B9915ECEE4A6A8134471E1C4F515C.dat
-rw-r--r--   1 isladm   mtnu      319949 Apr  4 15:03 1333541015642_SUBREGREQ_0392277106_7C2B9915ECEE4A6A8134471E1C4F515C.dat
-rw-r--r--   1 isladm   mtnu      319949 Apr  4 15:33 1333542817167_SUBREGREQ_0392277106_7C2B9915ECEE4A6A8134471E1C4F515C.dat
-rw-r--r--   1 isladm   mtnu      319949 Apr  4 16:03 1333544620699_SUBREGREQ_0392277106_7C2B9915ECEE4A6A8134471E1C4F515C.dat
-rw-r--r--   1 isladm   mtnu      319949 Apr  4 16:33 1333546435089_SUBREGREQ_0392277106_7C2B9915ECEE4A6A8134471E1C4F515C.dat
-rw-r--r--   1 isladm   mtnu      319949 Apr  4 17:34 1333550064879_SUBREGREQ_0392277106_7C2B9915ECEE4A6A8134471E1C4F515C.dat
-rw-r--r--   1 isladm   mtnu      319949 Apr  4 17:49 1333550942967_SUBREGREQ_0392277106_7C2B9915ECEE4A6A8134471E1C4F515C.dat
-rw-r--r--   1 isladm   mtnu      323597 Apr  4 08:41 1333518090276_SUBREGREQ_0392285992_48EFCA7F60DF45A0A5C23AA5BA24D791.dat
-rw-r--r--   1 isladm   mtnu      323597 Apr  4 10:44 1333525455685_SUBREGREQ_0392285992_48EFCA7F60DF45A0A5C23AA5BA24D791.dat
-rw-r--r--   1 isladm   mtnu      323597 Apr  4 10:56 1333526211742_SUBREGREQ_0392285992_48EFCA7F60DF45A0A5C23AA5BA24D791.dat
-rw-r--r--   1 isladm   mtnu      323597 Apr  4 12:31 1333531874975_SUBREGREQ_0392285992_48EFCA7F60DF45A0A5C23AA5BA24D791.dat
-rw-r--r--   1 isladm   mtnu      323597 Apr  4 13:03 1333533821849_SUBREGREQ_0392285992_48EFCA7F60DF45A0A5C23AA5BA24D791.dat
-rw-r--r--   1 isladm   mtnu      323597 Apr  4 13:40 1333536012802_SUBREGREQ_0392285992_48EFCA7F60DF45A0A5C23AA5BA24D791.dat
-rw-r--r--   1 isladm   mtnu      323597 Apr  4 14:13 1333538012578_SUBREGREQ_0392285992_48EFCA7F60DF45A0A5C23AA5BA24D791.dat
-rw-r--r--   1 isladm   mtnu      323597 Apr  4 14:28 1333538918666_SUBREGREQ_0392285992_48EFCA7F60DF45A0A5C23AA5BA24D791.dat
-rw-r--r--   1 isladm   mtnu      323597 Apr  4 15:17 1333541832704_SUBREGREQ_0392285992_48EFCA7F60DF45A0A5C23AA5BA24D791.dat

4. You can see many file contains the same number like "0312102619" that came from grep in previous command

5. I want "only" one file name that has "newest " date and time, selected from many files name that contains the same number (Uniquely).

Thank you.
MSTEST.txt
MYLIST.txt
0
Comment
Question by:trazodone
  • 2
4 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 37814133
Add

 | tail -1

after

| grep " $1

awk '{print "ls -lrt | grep "$1" | tail -1"}' MSTEST.txt | sh

Use "ls -rt" (without "l") to get just the filename.

I modified your awk statement a bit more, to avoid "cat"

wmp
0
 
LVL 48

Expert Comment

by:Tintin
ID: 37814824
No need for cat, or awk.

Simply do

ls -t|grep -f MSTEST.txt |tail -1

Open in new window


or if you want the full file details

ls -lt|grep -f MSTEST.txt |tail -1

Open in new window

0
 

Author Closing Comment

by:trazodone
ID: 37815513
Thank you.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 37817689
Why on earth would you want to use awk and pipe to sh?   It's totally unnecessary.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question