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

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.
osiris [ Mi, 02 April 2008 10:52 ] [ ID #1933522 ]

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

osiris [at] 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.
PK [ Mi, 02 April 2008 11:14 ] [ ID #1933523 ]

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

pk <pk [at] pk.invalid> wrote:
>osiris [at] 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.
osiris [ Mi, 02 April 2008 11:28 ] [ ID #1933524 ]

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

osiris [at] 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.
PK [ Mi, 02 April 2008 11:45 ] [ ID #1933525 ]

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

On 2 Apr., 11:28, osi... [at] abydos.kmt wrote:
> pk <p... [at] pk.invalid> wrote:
> >osi... [at] 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.
Janis Papanagnou [ Mi, 02 April 2008 11:48 ] [ ID #1933526 ]

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

osiris [at] 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.
PK [ Mi, 02 April 2008 12:16 ] [ ID #1933528 ]

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

pk <pk [at] pk.invalid> wrote:
>osiris [at] 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.
osiris [ Mi, 02 April 2008 12:09 ] [ ID #1933529 ]

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

osiris [at] 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.
PK [ Mi, 02 April 2008 12:58 ] [ ID #1933531 ]

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

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.
PK [ Mi, 02 April 2008 13:04 ] [ ID #1933532 ]
Linux » comp.unix.shell » how to specify all files of a certain string length in Bourne Shell

Vorheriges Thema: Re: email from shell with htm attachment
Nächstes Thema: Re: Get the parameter pattern from command line