MLUG: Re: [MLUG] Tcsh Programming (sed, awk, grep)
Re: [MLUG] Tcsh Programming (sed, awk, grep)
Email address obfuscation in effect -- please click here to turn it off.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
On Tue, 1 Oct 2002, Davis, Ryan Wiley (UMC-Student) wrote:

> I take a file and parse through it with grep looking for a certain entry
> in the file. When that line is found I want to remove said line from the
> file. Example in code is here:
>
> <code>
>
> set foo = `grep $eatme now.file`
> sed -n '/^\"$foo\"/d' now.file
>
> I also tried:
> sed -n '/^\$foo\/d' now.file
> and
> sed -n '/^$foo/d' now.file
> </code>
>
> Now grep is taking that entry (from variable $eatme) and storing it in
> "foo", but I can't pass that variable into sed.

First, be careful with 'set'.  I'm not sure it will work with the spaces
around the equal sign.  We definitely had tcsh scripts fail just last
night in my class because of that very thing (the space *after* the equal
sign was causing errors).  So I think you should have it look like this:

set foo=`grep $eatme now.file`

But if more than one line matches for grep, I'm not so sure that will
work.  You can only have a one-line string in foo, no?

When you use single quotes, the variable is not read as a variable -- the
'$' charaster is interpretted literally.  I think you can make something
like this work:

sed -n "/^$foo/d" now.file

But, you might not know that "grep -v" will delete the matching lines
instead of displaying them.  So maybe your problem is actually very easily
solved by doing this:

grep -v $eatme now.file

Let us know if this works.

Mike

--
To unsubscribe, go to http://mlug.missouri.edu/members/edit.php

Archives are available at http://mlug.missouri.edu/list-archives/