Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-01-2008, 02:17 PM   #1
LQ Newbie
Registered: Mar 2008
Posts: 1

Rep: Reputation: 0
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?
Old 03-01-2008, 02:27 PM   #2
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910

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?

Old 03-01-2008, 02:27 PM   #3
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
grep from filename.txt can't get the whole line papasj Programming 3 02-25-2008 01:38 PM
Grep result passed to variable cornish Programming 5 11-15-2007 06:05 PM
How to use grep to search for a specific variable.. memo007 Linux - Software 4 02-24-2007 06:52 PM
how to use environment variable as part of filename beeblequix Linux - Newbie 3 07-11-2006 05:55 PM
bash: how to edit cat <filename> | grep <keyword> feature? sirpelidor Linux - Software 2 06-20-2005 03:00 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:11 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration