how to specify all files of a certain string length in Bourne Shell

how to specify all files of a certain string length in Bourne Shell

am 02.04.2008 10:52:12 von osiris

I have a slew of files in one directory where
most of the filenames are 4 characters long.
How can I use the Bourne shell (only)
to specify those 4-character filenames?

I can't use an asterisk since some files are
longer than 4 characters.

I can't use any other shell other than the Bourne shell
on this particular computer.

The 4-char filenames are all numeric so I tried

grep "$re" "$dir/[0-9][0-9][0-9][0-9]"

but the Bourne shell claims:

No such file or directory

Thanks for your help.

Re: how to specify all files of a certain string length in Bourne Shell

am 02.04.2008 11:14:21 von PK

osiris@abydos.kmt wrote:

> How can I use the Bourne shell (only)
> to specify those 4-character filenames?

$ ls
11111 2222 33333 4444 55555 file1 file2 file3 moo
$ echo ????
2222 4444

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.

Re: how to specify all files of a certain string length in Bourne Shell

am 02.04.2008 11:28:21 von osiris

pk wrote:
>osiris@abydos.kmt wrote:
>
>> How can I use the Bourne shell (only)
>> to specify those 4-character filenames?
>
>$ ls
>11111 2222 33333 4444 55555 file1 file2 file3 moo
>$ echo ????
>2222 4444

Ooops! I did use that but then I discovered that there were
files that were non-numeric that were also 4 characters long
so then I tried "[0-9][0-9]??" and from there went to
"[0-9][0-9][0-9][0-9]" -- so I still don't have a solution
unless I use a loop which I was hoping not to do.

Many thanks.

Re: how to specify all files of a certain string length in Bourne Shell

am 02.04.2008 11:45:40 von PK

osiris@abydos.kmt wrote:

> Ooops! I did use that but then I discovered that there were
> files that were non-numeric that were also 4 characters long
> so then I tried "[0-9][0-9]??" and from there went to
> "[0-9][0-9][0-9][0-9]" -- so I still don't have a solution
> unless I use a loop which I was hoping not to do.

It works regardless of numeric/alphabetic characters:

$ ls
1111 45gh abcd dfg67 mn18 ppppp s\ ss
$ echo ????
1111 45gh abcd mn18 s ss

It would help, however, if you told us what you're trying to do exactly.

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.

Re: how to specify all files of a certain string length in Bourne

am 02.04.2008 11:48:40 von Janis Papanagnou

On 2 Apr., 11:28, osi...@abydos.kmt wrote:
> pk wrote:
> >osi...@abydos.kmt wrote:
>
> >> How can I use the Bourne shell (only)
> >> to specify those 4-character filenames?
>
> >$ ls
> >11111 =A02222 =A033333 =A04444 =A055555 =A0file1 =A0file2 =A0file3 =A0moo=

> >$ echo ????
> >2222 4444
>
> Ooops! =A0I did use that but then I discovered that there were
> files that were non-numeric that were also 4 characters long
> so then I tried "[0-9][0-9]??" and from there went to
> "[0-9][0-9][0-9][0-9]" -- so I still don't have a solution
> unless I use a loop which I was hoping not to do.

Don't include the [0-9][0-9]?? inside the quotes; the shell won't
expand it there. Instead...

grep "$re" "$dir"/[0-9][0-9]??


Janis

>
> Many thanks.

Re: how to specify all files of a certain string length in Bourne Shell

am 02.04.2008 12:09:18 von osiris

pk wrote:
>osiris@abydos.kmt wrote:
>
>> Ooops! I did use that but then I discovered that there were
>> files that were non-numeric that were also 4 characters long
>> so then I tried "[0-9][0-9]??" and from there went to
>> "[0-9][0-9][0-9][0-9]" -- so I still don't have a solution
>> unless I use a loop which I was hoping not to do.
>
>It works regardless of numeric/alphabetic characters:
>
>$ ls
>1111 45gh abcd dfg67 mn18 ppppp s\ ss
>$ echo ????
>1111 45gh abcd mn18 s ss
>
>It would help, however, if you told us what you're trying to do exactly.


The command that I am issuing is:

head -1 $dir/results/???? | grep "^[12]" > outfile

and the resulting error is:

/home/osiris/od/results/????: No such file or directory

My first line contains:

#! /bin/sh -f

I now notice that I get the error whether I use
????, [0-9][0-9]?? with or without quotes.

I'm using Solaris 7.

Re: how to specify all files of a certain string length in Bourne Shell

am 02.04.2008 12:16:15 von PK

osiris@abydos.kmt wrote:

>>$ ls
>>11111 2222 33333 4444 55555 file1 file2 file3 moo
>>$ echo ????
>>2222 4444
>
> Ooops! I did use that but then I discovered that there were
> files that were non-numeric that were also 4 characters long
> so then I tried "[0-9][0-9]??" and from there went to
> "[0-9][0-9][0-9][0-9]" -- so I still don't have a solution
> unless I use a loop which I was hoping not to do.

Ah, maybe I understand now. You want only 4-char long, all-numeric
filenames, right?

Then:

$ ls ????
1111 45gh 5555 abcd mn18
$ ls ???? | grep '[[:digit:]]\{4\}'
1111
5555

Another option is:

$ echo [[:digit:]][[:digit:]][[:digit:]][[:digit:]]
1111 5555

You could probably safely use [0-9] in place of [:digit:].

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.

Re: how to specify all files of a certain string length in Bourne Shell

am 02.04.2008 12:58:08 von PK

osiris@abydos.kmt wrote:

> The command that I am issuing is:
>
> head -1 $dir/results/???? | grep "^[12]" > outfile
>
> and the resulting error is:
>
> /home/osiris/od/results/????: No such file or directory
>
> My first line contains:
>
> #! /bin/sh -f
>
> I now notice that I get the error whether I use
> ????, [0-9][0-9]?? with or without quotes.
>
> I'm using Solaris 7.

I'm not sure what the -f option to sh does in solaris, hopefully someone
else will explain that (to me also!).
Do you get the same results if you run the command from the command line?
What shell are you using (ie, what does "echo $SHELL" return)?

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.

Re: how to specify all files of a certain string length in Bourne Shell

am 02.04.2008 13:04:29 von PK

pk wrote:

> I'm not sure what the -f option to sh does in solaris, hopefully someone
> else will explain that (to me also!).

With bash, running "bash -f" seems to have the same effect as
running "set -f", ie disabling pathname expansion.
Try removing the -f argumento to sh from the beginning of your script.

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.