Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Shell script to select newest file name from their group

Posted on 2012-04-05
4
Medium Priority
?
549 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Utilizing an array to gracefully append to a list of EmailAddresses
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to moveā€¦
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

721 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