Solved

Shell programing and regular expressiong: -> grep

Posted on 2003-11-24
9
394 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 250 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
 
LVL 24

Expert Comment

by:shivsa
ID: 9813416
did ot work, please update.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

746 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

14 Experts available now in Live!

Get 1:1 Help Now