-   Programming (
-   -   Using a variable containing a filename in grep (

TrumpetMan258 03-01-2008 02:17 PM

Using a variable containing a filename in grep
I'm working on a shell script that counts the number of each html tag in a file. Here's the part I'm having trouble with:


while getopts "df:" param
    if test '$param = d'; then
    elif test '$param = f';  then

echo $delim
echo $files

grep -oh '<[a-zA-Z0-9]\+\( [a-zA-Z0-9]\+=".\+"\)*/\?>' $files | tee list.tmp | sort -u  | tee sortlist.tmp | xargs -I% grep -c % list.tmp > numbers.tmp

If, for example, I call "./tagstats -f project2.html", the while loop successfully gets the filename and the "echo $files" successfully prints it out, but when I try to use the files variable in grep, the script just hangs at the command line. In other words, grep doesn't do anything. But if I hard code the filename into the command, it does work. How can I use that files variable in grep?

Tinkster 03-01-2008 02:27 PM


And welcome to LQ!

And all I can say is your script works for me. Not that it's
results mean much to me. Are you feeding it a strange file-name
by any chance?


ta0kira 03-01-2008 02:27 PM

Using ' will prevent $param from being replaced with its value. You need to use " instead.

All times are GMT -5. The time now is 10:37 AM.