Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1058
  • Last Modified:

wildcards in korn shell

i have a list of files that I want to operate on my script that uses
korn shell(ksh) like these:
p0011600
p0012400
p0012500
p0023800
p0022500
p0035800

I would like to use wildcards to select all those files that I want
like these:

p002*            selects those that begins with p002
*800            selects those that ends with 800
p00[!2]*      selects all except those that begins with p002
p0022500      selects p0022500

My question is, what if I want the opposite of the last example.  I
would like to choose all except p0022500.  I have tried many
combinations like issuing p00[!2][2][5]00 but it doesn't work.

Also, can I combined two conditions like: choosing all except
p0022500 and p0035800.  Something like (!p0022500 && !p0035800).

I am using korn shell(ksh).  Thank you.
0
jimtouzo
Asked:
jimtouzo
  • 5
  • 3
  • 2
  • +2
1 Solution
 
markus_baertschiCommented:
You could operate in a loop and filter out unwanted files as follows:

for FILE in p*
do
  if ...  #condition for files to skip
    continue
  fi
  whatever-command $FILE
done

Using the ${FILE%regex} or ${FILE#regex} constructs you
can match whatever you want in the if clause. You'll find more
details about these constructs on:
http://www.rs6000.ibm.com/doc_link/en_US/a_doc_lib/aixuser/usrosdev/parameter_subst.htm
(This is from IBM's AIX doc)

Markus
0
 
ferguswilsonCommented:
Jim,
Using egrep will do the trick for you...take the following as an example;

egrep -v '(p00[2][0-9]|p0011600)' $LIST

the -v option is an exclusion list for;
all matches of p00, 2, and then any amount of occurences of 0 through 9.
Seperate each condition with the bar symbol |

So including (or not) the -v option gives mirrored results.

Fergus.
0
 
ferguswilsonCommented:
In error, I posted this as an answer, not a comment (changed it now!)...sorry all.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
PC_User321Commented:
* after [0-9]?
0
 
festiveCommented:
#!/bin/sh
# exclude PATTERNS FROM DIR and process
# all other files
# ie a pattern such as fergus's should work
# PAT1-3 could be variables also

for filetodo in `ls|egrep -v "PAT1|PAT2|PAT3"; do
     echo "processing $filetodo"
     # commands go here
done
0
 
ferguswilsonCommented:
festive,
I don't think that posting something that I've already stated should be your answer to the original question.
It should be a comment instead (please).

PC User123,
p00[2][0-9] is the equivelent of p002*
(in regualr expression terms versus normal shell wildcards)

jimtouzo,
Here are the statements that answer your original question more exactly...
egrep -v '(p0022500|p0035800)' $LIST
(all except p0022500 and p0035800)

egrep -v '(p00[2][0-9])' $LIST
(all except p002*)

egrep  '(800$|500$)' $LIST
(only ones ending with 800 or 500)

Regards,
Fergus.
0
 
festiveCommented:
Maybe, but I believe that my answer is:

1) far more concise and straightforward.
2) easier to read and understand.
3) more complete.

and after all, are we not here to help ??
so in the spirit of competition and altruism here is the copmpleted script:

#!/bin/ksh
# cold_shoulder.sh
#         a program to process some files, but
#         ignore others (like answers) <grin>
#
# below are the patterns to ignore:
# note: if they are not needed , remove from
# below and the egrep line.
#
P1="p002"
P2=""
P3=""
#
for filetodo in `ls|egrep -v "$P1|$P2|$P3"; do
    echo "processing $filetodo"
    # commands go here
done
0
 
ferguswilsonCommented:
I agree with you that it's more clear to read & more concise...but;

have you actually read the original question ?
What is being asked for, is not a pretty script that uses the most basic egrep command, but he's asking for help with determining how to exclude certain specific conditions (using wildcards).

3) more complete
? eh ? where do you mention anything about using wildcards in your "complete" script
(i notice this time you actually made it a korn shell !)

Let our answers decide for us, in the spirit of competition !!
0
 
jimtouzoAuthor Commented:
Thanks for your answers guys.

But I think ferguswilson answered exactly what I want to hear.  I would like to find out how can I separate particulars from the list using wildcards.  Surely, festive's answer is one approach.  I thought of that before, but I think it's less direct to the point and slower because the program has to do a lot of loopings on big lists.

another thing, why do you insist on using egrep instead of grep.  What's their differences?

ferguswilson:
how can I credit the points to you?
0
 
jimtouzoAuthor Commented:
Thanks ferguswilson

Can you still send me your answer about my question on the difference between grep and egrep?
0
 
festiveCommented:
touche' , and in the words of DARK HELMET:
"so, I see your schwartz is as big as mine" :-)
0
 
ferguswilsonCommented:
Jimtouzo,
Thanks for the points...glad you saw the error of the "festive" way !!
(festive; it's been fun locking horns !)

Jim,
Extended grep (or egrep) let's you group a number of conditions within the brackets, but the older (or original version of the program) doesn't have this option. Also the switch of -v for exclude rather than include can be a pretty powerfull option. There's also a number of other grep's that can be used with slightly different options. All depends on your needs (i.e. agrep, fgrep). Check what your version of unix has...

This answer your additional question ?
Regards,
Fergus.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now