Patterns in Filenames


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Patterns in Filenames
# 1  
Old 08-01-2008
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.
# 2  
Old 08-01-2008
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...
# 3  
Old 08-01-2008
Something like:

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

Last edited by dcfargo; 08-01-2008 at 05:03 PM..
# 4  
Old 08-01-2008
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)?
# 5  
Old 08-01-2008
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.
# 6  
Old 08-01-2008
- 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
# 7  
Old 08-01-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sort the filenames

Hello Unix experts: I have dir where few files are there, i want to sort these files and write the output to some other file but i need filenames with filepath too eg: i have filenames like 010020001_S-FOR-Sort-SYEXC_20171218_094256_0004.txt so i want to sort my files on first 5 fields of... (2 Replies)
Discussion started by: gnnsprapa
2 Replies

2. Shell Programming and Scripting

Bash - Find files excluding file patterns and subfolder patterns

Hello. For a given folder, I want to select any files find $PATH1 -f \( -name "*" but omit any files like pattern name ! -iname "*.jpg" ! -iname "*.xsession*" ..... \) and also omit any subfolder like pattern name -type d \( -name "/etc/gconf/gconf.*" -o -name "*cache*" -o -name "*Cache*" -o... (2 Replies)
Discussion started by: jcdole
2 Replies

3. Shell Programming and Scripting

Rename Filenames

Hi there I have thousands files like: SG1130113000247.CAPNFXS SG1130113001247.CAPNFXT SG1130113002247.CAPNFXU . . . I want to remove SG1 and .CAP* from file name, and rename it to: 130113000247 130113001247 130113002247 (9 Replies)
Discussion started by: ali.seifaddini
9 Replies

4. Shell Programming and Scripting

Find matched patterns and print them with other patterns not the whole line

Hi, I am trying to extract some patterns from a line. The input file is space delimited and i could not use column to get value after "IN" or "OUT" patterns as there could be multiple white spaces before the next digits that i need to print in the output file . I need to print 3 patterns in a... (3 Replies)
Discussion started by: redse171
3 Replies

5. Slackware

cp does not like filenames with accents?

Hi: mkisofs -graft-points -rational-rock -joliet -joliet-long -full-iso9660-filenames -iso-level 2 -o /tmp/image.iso STORE1/=/almacen/strauss In /almacen/strauss there are filenames containing not only spaces but accented characters as well. I burned the image to DVD, with the result that all... (2 Replies)
Discussion started by: stf92
2 Replies

6. Shell Programming and Scripting

get filenames from log

Hi. I'm trying to get the names of files from a log file, without the path and special characters. I have a file that contains lines like this: '/path/to/files/file00010000070874.EXT' '/path/to/files/file00010000070875.EXT' '/path/to/files/file00010000070876.EXT'... (4 Replies)
Discussion started by: Hekm
4 Replies

7. AIX

filemon with no filenames...

i excuted filemon with filemon -u -o /tmp/filemon.out -O all;sleep 60; trcstop. everything is ok, but i only get PID for filenames in Most Active Files. is there any different flags i need to use to get filenames? Code tags please, thanks. (3 Replies)
Discussion started by: curtis911
3 Replies

8. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies

9. UNIX for Dummies Questions & Answers

parsing filenames

How can I loose a part of the filename I want to drop the “_<Number>.sql” Below I have a listing of file names in a file Eg : CREDIT_DEL_033333.sql I want it to be CREDIT_DEL ATM_DEBIT_CARD_0999999.sql I want it to be ... (3 Replies)
Discussion started by: jville
3 Replies

10. Shell Programming and Scripting

spaces in filenames

I have a problem with the script below #!/bin/sh for vo in `find -maxdepth 1 -type f -regex "^\./*$"` do ls -l "$vo" some other commands done It works fine until `find ...` returns files with spaces. I've tryed to change IFS but haven't succeed Any solutions? (4 Replies)
Discussion started by: Hitori
4 Replies
Login or Register to Ask a Question