Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 555
  • Last Modified:

Shell script to select newest file name from their group

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
trazodone
Asked:
trazodone
  • 2
1 Solution
 
woolmilkporcCommented:
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
 
TintinCommented:
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
 
trazodoneAuthor Commented:
Thank you.
0
 
TintinCommented:
Why on earth would you want to use awk and pipe to sh?   It's totally unnecessary.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now