[lug] Simple (?) bash+grep issue

Collins Richey crichey at gmail.com
Wed May 11 19:09:15 MDT 2005

On 5/11/05, Jeff Schroeder <jeff at neobox.net> wrote:
> 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?

/me ducks after replying

Use perl, where a simple 'chop $item' will cure the problem.

       When I saw the Iraqi people voting three weeks ago, 8 million of them, 
       it was the start of a new Arab world.... The Berlin Wall has fallen. 
               - Lebanese Druze leader Walid Jumblatt

More information about the LUG mailing list