?
Solved

Shell programing and regular expressiong: -> grep

Posted on 2003-11-24
9
Medium Priority
?
408 Views
Last Modified: 2010-04-22
Ok, what I need to do is: list all files in a given directory with a given extension

$1 = directory
$2 = extension

I want to use regular expression for this, so I used the command
filelist = `ls $1 | grep '.*[\\\\.]$2$'`

the problem is, the $ from $2 conflicts with the $ from the regular expression

What I've already tried:
- putting the command in a dummy variable as a string and then execute it
- putting all sort of quotes around it
- banging my head on the table

PS: it works if I replace $2 with the actual extension
This is a school assignment so I can't use any other language or so

Hope someone can help me,

thanks,

Walter
0
Comment
Question by:FrEaK85
9 Comments
 
LVL 7

Accepted Solution

by:
glassd earned 750 total points
ID: 9812215
Why not just use:
filelist=`ls $1/*.$2`

More efficient than calling another process (grep).
0
 
LVL 24

Expert Comment

by:shivsa
ID: 9812773
filelist = `ls $1 | grep ".*[\\\\.]$2$''`
0
 
LVL 24

Expert Comment

by:shivsa
ID: 9812789
also after setting $1 and $2, do not forget to export them.
and run the above command, it is working onmy system.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 24

Expert Comment

by:shivsa
ID: 9813416
did ot work, please update.
0
 
LVL 7

Expert Comment

by:glassd
ID: 9816928
Patience.

Still don't see why you need to invoke grep when ls was written to do what you want.
0
 

Expert Comment

by:S_KuMaaaaR
ID: 9817169
try it using eval before grep as follows

var1=directory
var2=extension

filelist=`ls | eval grep '.*[\\\\.]$var2$'`

I think, this  should work

rgds,
Sanjeev
0
 
LVL 38

Expert Comment

by:yuzh
ID: 9822102
You don't need to use grep, just simply use:

filelist=`/bin/ls ${1}/*.${2}`

PS: use the full path to ls (do a which ls to find out), in case you have
      set aliase for ls.

0
 
LVL 20

Expert Comment

by:jmcg
ID: 9822293
Actually, there's no need to use 'ls', either. Expanding wildcards (sometimes called globbing) is something the shell does and in all of these formulations using ls, it's really the shell doing the work.

filelist=$(echo $1/*.$2)

As for the way to get your original formulation to work, you need to protect the $ that was intended to be an end-of-string marker from being prematurely taken by the shell as a variable substitution. You seemed to have some concept of this given the number of backslashes you put before that . in the square brackets...but here's a version that worked for me:

filelist=`ls $1 | egrep ".*\\.$2\$"`

The various shells differ a bit in whether variable interpolation takes place inside single quotes inside backticks. To be safe, I stuck to double-quotes.

0
 

Author Comment

by:FrEaK85
ID: 9825273
filelist=`ls $1/*.$2` => feeling stupid stupid stupid
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

850 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