Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ls command

Status
Not open for further replies.

Ambatim

Programmer
Feb 28, 2002
110
IN
Hi

I have three files in my directory
SEAR_BM
SEAR_MM
SEAR_ME

I want to select only SEAR_BM and SEAR_ME.

If I give 'ls SEAR_[BM][ME]' it selects all the three files instead of two.

How to give the ls command so that it selects only SEAR_BM and SEAR_ME.

Any Ideas Thanks in advance
Mallik
 
in ksh:

ls SEAR_@(BM||ME) vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
dchoulette - what's the character between BM & ME in your answer & how do I get it on my keyboard?


Mallik - I suppose if you already know what files you want to omit you could just do:

ls SEAR_* | grep -v MM$

I can't think of a way to make it more general than that though.

 
The character between the fields is simply a comma ','. The fields are surrounded by curly braces '{' and '}'. There is no space between '{', ',', '}' and the fields.
I tried it on all shells available on my AIX: sh, ksh, csh and bash. It works for sh, csh and bash but not for ksh (?).

I got this from man csh:
Code:
The metanotation a{b,c,d}e is a shorthand for
&quot;abe ace ade&quot;.
Left to right order is preserved, with results of matches
being sorted separately at a low level to preserve this
order.
This construct may be nested.  Thus:

     ~source/s1/{oldls,ls}.c

expands to

     /home/source/s1/oldls.c /home/source/s1/ls.c

whether or not these files exist, without any chance of
error if the home directory for source is /home/source.
Similarly,

      ../{memo,*box}

might expand to

      ../memo ../box ../mbox

(Note that memo was not sorted with the results of
matching *box.) As a special case, {, }, and {} are
passed undisturbed.

And this from man bash:
Code:
   Brace Expansion
Brace expansion is a mechanism by which arbitrary
strings may be generated.  This mechanism is similar
to pathname expansion, but the filenames generated
need not exist.  Patterns to be brace expanded take
the form of an optional preamble, followed by a series
of comma-separated strings between a pair of braces,
followed by an optional postscript.  The preamble is prefixed to each string contained within the braces,
and the postscript is then appended to each resulting
string, expanding left to right.

Brace expansions may be nested.  The results of each
expanded string are not sorted; left to right order
is preserved.  For example, a{d,c,b}e expands into
`ade ace abe'.

Brace expansion is performed before any other
expansions, and any characters special to other
expansions are preserved in the result.
It is strictly textual.  Bash does not apply any
syntactic interpretation to the context of the expansion
or the text between the braces.

A correctly-formed brace expansion must contain
unquoted opening and closing braces, and at least
one unquoted comma.  Any incorrectly formed brace
expansion is left unchanged.  A { or , may be quoted
with a backslash to prevent its being considered part
of a brace expression.  To avoid conflicts with
parameter expansion, the string ${ is not considered
eligible for brace expansion.

This construct is typically used as shorthand when
the common prefix of the strings to be generated is
longer than in the above example:

   mkdir /usr/local/src/bash/{old,new,dist,bugs}
or
   chown root /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}}
 
Thanks - I was trying it in ksh & as you say it's not supported there.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top