- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: last file name having numbers in names
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
Forums
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
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
06-01-2008 07:53 PM
06-01-2008 07:53 PM
I have few files with say below names:
a.d5
a.d4
a.d3
a.d2
a.d1
I would like to know the file name with biggest number in its extension, a.d5 in above case. Is there a way I could do that?
Please suggest. Thank you!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2008 08:27 PM
06-01-2008 08:27 PM
Re: last file name having numbers in names
Probably hundreds of ways.
This looks like a very poorly specified
problem, but, as stated:
td191> ls -l
total 0
-rw-rw-rw- 1 antinode 513 0 Jun 2 00:24 a.d1
-rw-rw-rw- 1 antinode 513 0 Jun 2 00:24 a.d2
-rw-rw-rw- 1 antinode 513 0 Jun 2 00:24 a.d3
-rw-rw-rw- 1 antinode 513 0 Jun 2 00:24 a.d4
-rw-rw-rw- 1 antinode 513 0 Jun 2 00:24 a.d5
td191> ls -1 | tail -1
a.d5
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2008 08:30 PM
06-01-2008 08:30 PM
Re: last file name having numbers in names
let me add little more to the problem. I have files names from a.d1 to a.d15 and i want to have the output of a.d15 , which means the name having biggest number in the end of that file?
Please suggest.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2008 08:31 PM
06-01-2008 08:31 PM
Re: last file name having numbers in names
# ls a.d* | tail -1
However, add a file a.d10 and that breaks.
Here is a perl based solution which takes the filename apart and compares the numbers at the end.
$ perl -le 'foreach (glob ("a.d*")) {$x = $1 if /(\d+)$/; $max=$x if $x>$max} print "Max = $max"'
Or with a traditional pipe from ls:
# ls x.d* | perl -nle '$x = $1 if /(\d+)$/; $max=$x if $x>$max}{ print "Max = $max"'
For a better solution, please provide a better problem description.
For example... is the extention always a d followed by a number? Leading zeroes? Other files with an extention starting with ".d" (.data). Always just one period? ...
hth,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2008 09:04 PM
06-01-2008 09:04 PM
SolutionMy first perl suggestion prints just the highest number, which I suspect is what you really want.
But the request is to print the name for the file with the highest number. Try this:
ls a.d* | perl -nle 'if (/(\d+)$/ && $1>$max){$max=$1; $name=$_} }{ print "Max = $name"'
Now if there is only 1 "d" in the name, and that "d" is just before the numbers, then you can use is as a seperator for sort:
$ ls a.d* | sort -td -k 2n | head -1
Enjoy,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2008 09:22 PM
06-01-2008 09:22 PM
Re: last file name having numbers in names
ls a.d* | sort -td -k 2n | tail -1
could you please let me know the significane of 2n after -k in sort command? Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2008 09:32 PM
06-01-2008 09:32 PM
Re: last file name having numbers in names
The right way to do this is to create filenames that sort alphanumerically the same as numerically. I.e. you plan ahead and add leading zeroes:
a.d001, a.d002, ...
(If you want, we can help with a script to do that.)
>could you please let me know the significance of 2n after -k in sort command?
The command -k2n says you have a key with the second field, and sort numerically:
-k keydef
The keydef argument defines a restricted sort key. The format of this definition is field_start[type][,field_end[type]]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2008 09:59 PM
06-01-2008 09:59 PM
Re: last file name having numbers in names
ls a.d* | sort -td -k 2n | tail -1
Thanks for your help!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2008 10:42 PM
06-01-2008 10:42 PM
Re: last file name having numbers in names
No, _a_ solution is...