Solved

wildcards in korn shell

Posted on 2000-04-02
12
1,003 Views
Last Modified: 2011-09-20
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
Comment
Question by:jimtouzo
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 1

Expert Comment

by:markus_baertschi
ID: 2679452
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
 

Expert Comment

by:ferguswilson
ID: 2680549
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
 

Expert Comment

by:ferguswilson
ID: 2680719
In error, I posted this as an answer, not a comment (changed it now!)...sorry all.
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2682629
* after [0-9]?
0
 
LVL 2

Expert Comment

by:festive
ID: 2682880
#!/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
 

Accepted Solution

by:
ferguswilson earned 85 total points
ID: 2683337
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:festive
ID: 2685833
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
 

Expert Comment

by:ferguswilson
ID: 2686745
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
 

Author Comment

by:jimtouzo
ID: 2687254
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
 

Author Comment

by:jimtouzo
ID: 2687268
Thanks ferguswilson

Can you still send me your answer about my question on the difference between grep and egrep?
0
 
LVL 2

Expert Comment

by:festive
ID: 2688226
touche' , and in the words of DARK HELMET:
"so, I see your schwartz is as big as mine" :-)
0
 

Expert Comment

by:ferguswilson
ID: 2698198
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now