Patterns in Filenames

Hi,

To start, I am using a bash shell on a G4 powerbook running Leopard. I am attempting to write a shell script that will automate the processing of satellite imagery. All the filenames are of the following construction:

A2008196000500.L2

where A indicates the sensor, the next four digits represent the year, the next three represent the julian day, and the remaining represent the hour, minute, and second of observation. All I am interested in are the three digits that specify the julian day, so my filenames are effectively:

?????ddd*.L2 (note it's not enough to say *ddd*, as the three digits of the julian day can match patterns in the rest of the filename)

The julian days of my files range from 001 to 366. I would like to be able to list all the files with julian days that range between two different values (ex all the files between julian day 3 and 10). How do I do this? It's not enough to type:

ls ?????[003-010]*.L2

Any help is greatly appreciated.

Have a look at the man page for egrep (extended-grep). That supports regular expressions. Then you could do something like:

ls -1 | egrep ?????(001|002|003...|010)*.L2

I've been away from shell script for too long (much better at perl regex at this point), but I think you can get egrep to work for you...

Something like:

ls -1 | awk '{if (2 < (substr($1, 6, 3) < 11) print $1}'

Hi dcfargo,

I am just getting familiar with unix. Would you mind explaining your awk statement. It appears that you are doing a greater than/less than comparison. Will that work if the days have 3 digits (ex 001, 002)?

You're right that doesn't seem to work. You may go with each integer as its own.

ls -1 | awk '{if (substr($1, 6, 1) =="0" && substr($1, 7, 1) == "0"... print $1}'

Their must be an easier way.

  • is that syntax for bash?

  • if it is possible to have an if statement within an awk statement i believe that would solve the problem. There must be a way similar to what you suggested earlier:

ls -1 | awk '{if (2 < (substr($1, 6, 3) < 11) print $1}'

only with the proper placement of the if statement within the awk, and the right syntax for bash

I think you'll need to do && for the two operations e.g.

if (2 < substr($1, 6, 3) && substr($1, 6, 3) < 11)

I'm also not certain its going to handle the 011 and 001 values correctly

I'd be tempted to just hammer it like:

ls -1| awk '{ if (substr($1, 6, 3) == "001" || substr($1, 6, 3) == "002" || substr($1, 6, 3) == "003" || ...) print $1 }'

Someone must know a good way to do this.