LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash scripting - largest # file extension (https://www.linuxquestions.org/questions/programming-9/bash-scripting-largest-file-extension-65948/)

brian0918 06-16-2003 12:36 PM

bash scripting - largest # file extension
 
I have files named bleh.1, bleh.2, bleh.3, bleh.4... bleh.10, etc. that are being created, and want to track their progress. Is there a way to find the largest extension (1, 2, 3, ... 10, etc) of the set of files named "bleh", then be able to use that extension to look at that file (if 10 is the largest then view bleh.10)?

note- the newest files will have the largest extension, so maybe there's a way to do it with some sort of search for the newest file, then grab its extension somehow, and use that extension with the set of files named "bleh" (ie: The newest file found is xyz.10, so look at bleh.10)

Thanks.

Hko 06-16-2003 12:47 PM

To view the newest file which name starts with "bleh":
Code:

less `ls -tr bleh* | tail -1`
To view the file with the highest number as its extension:
Code:

less bleh.`ls bleh* | sed 's/bleh.*\.\([0-9]*\)/\1/' | sort -n | tail -1`
Of course, you can use "more" or "cat" or "view" instead to view the file...

brian0918 06-16-2003 12:58 PM

Thanks for the reply.

Your suggestions work, but my problem now is that I'm trying to do this in a script, similar to this:

y=$(ls -tr bleh* | tail -1)

Now what I want to do is grep info from this file, using something like:

echo " `grep "blah" $y` "

I'm not sure, but I think I tried using this syntax before, and it didn't work. Is this the correct way to do it?

Thanks.

Hko 06-16-2003 01:06 PM

Quote:

Originally posted by brian0918
Now what I want to do is grep info from this file, using something like:

echo " `grep "blah" $y` "

[..snip..]Is this the correct way to do it?

Just:

grep "blah" $y

will do...


All times are GMT -5. The time now is 05:41 PM.