- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- sh script - grep files for string and only use lat...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2014 01:16 PM
01-06-2014 01:16 PM
I need to be able to search a directory for a string.
But, if there are multiple files that contain that string, only use the latest file.
I thought I had it, but it does not seem to work.
file=`grep -l ${tn} *| tail -1`
I tried head too, but that does not work either...
This is an HPUX server with #!/bin/sh
Solved! Go to Solution.
- Tags:
- grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2014 01:33 PM
01-06-2014 01:33 PM
Re: sh script - grep files for string and only use latest file
Try this:
file=$(grep -l ${tn} * | xargs ls -1tr | tail -1)
The 'xargs ls -1tr' (yes, that is the number one, NOT a lower-case L) will sort the files by last modification time in reverse order (newest last) and only print the file name on the line.
# man ls
for more details.
Also, it is better to use the $( ) syntax for commands rather than the back-ticks enclosing the commands. The $( ) is much easier to read.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2014 01:36 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2014 09:07 PM
01-08-2014 09:07 PM
Re: sh script - grep files for string and only use latest file
>The 'xargs ls -1tr' (yes, that is the number one
No real need to use -1 if you are sending the output of ls(1) to pipe or a file.
If you know you don't have zillions of files you can get away with: :-)
file=$(ls -t $(grep -l ${tn} *) | head -1)
Above is where you need to use $() vs ``, since nested.
- Tags:
- command substitution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2014 09:54 AM
01-09-2014 09:54 AM