Operating System - HP-UX
1833792 Members
1732 Online
110063 Solutions
New Discussion

Re: last file name having numbers in names

 
SOLVED
Go to solution
panchpan
Regular Advisor

last file name having numbers in names

Hello.
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!
8 REPLIES 8
Steven Schweda
Honored Contributor

Re: last file name having numbers in names

> Is there a way I could do that?

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
panchpan
Regular Advisor

Re: last file name having numbers in names

Thank you!
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.
Hein van den Heuvel
Honored Contributor

Re: last file name having numbers in names

If those are the relevant examples then the solution is a simple:

# 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.




Hein van den Heuvel
Honored Contributor
Solution

Re: last file name having numbers in names

Ah, more replies came in while I was typing/trying.

My 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.

panchpan
Regular Advisor

Re: last file name having numbers in names

Thank you - I got the expected result using below command:

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!
Dennis Handly
Acclaimed Contributor

Re: last file name having numbers in names

>I have files names from a.d1 to a.d15 and i want to have the output of a.d15

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]]
panchpan
Regular Advisor

Re: last file name having numbers in names

the solution is:
ls a.d* | sort -td -k 2n | tail -1

Thanks for your help!
Steven Schweda
Honored Contributor

Re: last file name having numbers in names

> the solution is:

No, _a_ solution is...