LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Using a variable containing a filename in grep (http://www.linuxquestions.org/questions/programming-9/using-a-variable-containing-a-filename-in-grep-624986/)

TrumpetMan258 03-01-2008 01: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:

Code:

while getopts "df:" param
do
    if test '$param = d'; then
        delim=$OPTARG
    elif test '$param = f';  then
        files=$OPTARG
    fi
done

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 01:27 PM

Hi,

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?


Cheers,
Tink

ta0kira 03-01-2008 01:27 PM

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


All times are GMT -5. The time now is 06:21 AM.