Solved

Shell script to select newest file name from their group

Posted on 2012-04-05
4
547 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

615 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