[lug] Simple (?) bash+grep issue

Jeff Schroeder jeff at neobox.net
Wed May 11 17:13:31 MDT 2005


Hey everyone--

I have a problem that must be simple to solve, but I can't figure out 
what it takes.  I've written a bash script that parses a file (with 
grep) to pick out certain lines, and then needs to run some shell 
commands using the values from those lines as arguments.  What's 
happening is I'm getting the newlines from the grep command, and those 
newlines are horking the subsequent shell command.

For example, I run the grep and save the results into a bash variable:

var=`grep "stuff" file.txt`

Then I step through the results to run my shell command:

for item in $var ; do echo $item ; done

Obviously this is a much-simplified example.  The problem is that $item, 
in the for-loop above, contains a trailing newline.  How do I remove 
the newline?  I poked around with awk and sed but didn't have any luck.  
I also thought about invoking a "perl -e" sort of command, but that 
seems overkill.  This shouldn't be that hard. :)

Any thoughts?

TIA,
Jeff



More information about the LUG mailing list