Solved

Shell programing and regular expressiong: -> grep

Posted on 2003-11-24
9
397 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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 fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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