View Full Version : substr function

August 18th, 2006, 08:09 PM
I am trying to replicate some BASIC functionality in Unix.

I have a loop which looks at all the files in a directory and culls them based on their names. Their names however vary and can only be "tested" by looking for a string within the file name.

So I want to test whether filename contains *lena* for example.

something like [ $filename = *lena ]

However the syntax eludes me.

Anyone know?


August 18th, 2006, 08:35 PM
In the syntax of BASIC I used the syntax for a substring was:

IF c$ = b$[1:4] THEN


but I'm slightly confused as to whether you are trying to write a BASIC interpreter/compiler or simply writing a program in a pre-existing framework?

August 18th, 2006, 08:59 PM
Sorry for anything misleading.

Since I did/do not know the Unix terminology I referred to BASIC's.

However I am trying to write a Unix script which will cull files having identified what to do with them from a (sub)string within the file name.

So, as I said, something like

for loop in 'ls -1'
if [ $loop = *str* ]; then
cull number1
if [ $loop = *str2* ]; then
cull number2

Sorry again

August 18th, 2006, 09:09 PM
Ahh this is a shell script ok. The syntax is:

substring=${string_variable_name:starting_position :length}

so for example:
bigstring="this is a test"

August 18th, 2006, 10:12 PM
I guess my memory of BASIC is not nearly as good as I thought it was. And substr is not what I remembered it as.

The sought string fragment is not in a known position so using a position, length statement isn't going to work I'm afraid.

I need to look for "str" somewhere, anywhere in the filename.

Sorry for all this messing about ...

August 18th, 2006, 10:43 PM
For the if statement, if you just want to identify a filename that contains "foo" at amy point:

for loop in 'ls -1'
if [ "$loop" = "*str*" ] ; then
#do some stuff
elif [ "$loop" = "*str2*" ] ; then
#do some stuff

I'm not sure what you mean by cull, do you mean delete? If so, the unix command is "rm". The syntax for the if statements is correct (though you will need to remove the #'s of course), but my loop knowledge is not very good (I'm also new to bash), so it may be wrong.

August 19th, 2006, 02:58 PM
Thanks for the response.

I think your code is right (insofar as no error messages result) but, I haven't been able to accomplish what I wanted. No I didn't want to delete the files, just move 'em around (depending on their name - or at least this "substr" in their name.

The script is:

cd .data/
for loop in "ls -1 *"
if [ "$loop" = "*abc*" ] ; then
cp $loop Desktop/First

A filename containing "abc" IS in the directory but no file gets copied. There are also many files that do NOT contain "abc" but changing = to != doesn't result in any files being copied either.

Perhaps my understanding that "loop" takes on the filenames one by one is the thing that's wrong.

Anyone know?