Solved

wildcards in korn shell

Posted on 2000-04-02
12
995 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
Comment Utility
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
Comment Utility
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
Comment Utility
In error, I posted this as an answer, not a comment (changed it now!)...sorry all.
0
 
LVL 5

Expert Comment

by:PC_User321
Comment Utility
* after [0-9]?
0
 
LVL 2

Expert Comment

by:festive
Comment Utility
#!/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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Expert Comment

by:festive
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
touche' , and in the words of DARK HELMET:
"so, I see your schwartz is as big as mine" :-)
0
 

Expert Comment

by:ferguswilson
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

763 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

8 Experts available now in Live!

Get 1:1 Help Now