?
Solved

Shell programing and regular expressiong: -> grep

Posted on 2003-11-24
9
Medium Priority
?
403 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

800 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