Solved

Shell script to select newest file name from their group

Posted on 2012-04-05
4
540 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now