Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

returning second numeric entry from ls 1

Status
Not open for further replies.

tonykent

IS-IT--Management
Jun 13, 2002
251
GB
I have a shell script to modify that was produced by a.n.other. If it was perl I could do it with no problems, but my shell scripting knowledge is not up to the task. I think I'll need to use awk, hence this posting.

I have a directory, the top few entries in it being subdirectories called:

history/
exceptions/
070221/
070220/
070219/

The script uses the output of:

Code:
ls -1r /directory_name | head -4 | tail -1

to provide me with yesterday's subdirectory i.e. 070220.

This works fine except when someone has been testing and the top few entries become:

history/
exceptions/
070221/
070220tests/
070220/
070219/

The script fails if '070220tests/' is returned.

I always want to receive the second all-numeric entry i.e 070220 in the above list.

Can this be done using awk?
 
how about
Code:
ls -1r /dirname|grep '^[0-9][0-9][0-1][0-9][0-3][0-9]/'|head -2|tail -1


HTH,

p5wizard
 
Excellent p5wizard. That's much simpler than the convoluted awk I'd been struggling with. A * for you.
 
I'm sure someone will come up with a lean awk solution -without the head and tail - this being an awk forum and all...

But this works just fine.


HTH,

p5wizard
 
So, one awk way:
ls -1r /dirname | awk '/^[0-9]{6}\//{if(++f==2){print;exit}}'

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks PHV, I will give that a look today.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top