[comp.unix.questions] shell script question

jharkins@sagpd1.UUCP (Jim Harkins) (03/03/90)

I have a list of files in which I want to change the word 'foo' to the filename.
What I tried to do was

foreach i (list of files)
	sed 's/foo/$i/' < $i > tmp
	mv tmp $i
end

But what this does is replace 'foo' with '$i', not the filename.  Can anybody
help?  I'm running 4.3 BSD UN*X.  Thanks.

-- 
jim		jharkins@sagpd1

"I've found by and large that when the flu gets you, the best thing to do (other
 than go to bed, which is boring) is to go conquer something." -Jerry Pournelle

merlyn@iwarp.intel.com (Randal Schwartz) (03/04/90)

In article <652@sagpd1.UUCP>, jharkins@sagpd1 (Jim Harkins) writes:
| I have a list of files in which I want to change the word 'foo' to the filename.
| What I tried to do was
| 
| foreach i (list of files)
| 	sed 's/foo/$i/' < $i > tmp
| 	mv tmp $i
| end
| 
| But what this does is replace 'foo' with '$i', not the filename.  Can anybody
| help?  I'm running 4.3 BSD UN*X.  Thanks.

Single quotes prevent the CSH from interpreting '$i'.  Your solution
using csh/sed is:

foreach i (list of files)
	sed "s/foo/$i/" <$i >tmp
	mv tmp $i
end

My one-liner solution using Perl is:

perl -pi -e 's/foo/$ARGV/;' list of files

Just another Perl hacker,
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/

gwyn@smoke.BRL.MIL (Doug Gwyn) (03/04/90)

In article <652@sagpd1.UUCP> jharkins@sagpd1.UUCP (Jim Harkins) writes:
>	sed 's/foo/$i/' < $i > tmp
>But what this does is replace 'foo' with '$i', not the filename.

Not surprising when you consider that the shell does not perform variable
substitution within ''-quoted strings.  Use "" instead, or arrange for the
$i to be outside the quotes.

tkevans@fallst.UUCP (Tim Evans) (03/04/90)

In article <652@sagpd1.UUCP>, jharkins@sagpd1.UUCP (Jim Harkins) writes:
> I have a list of files in which I want to change the word 'foo' to the filename.
> What I tried to do was
> 
> foreach i (list of files)
> 	sed 's/foo/$i/' < $i > tmp
> 	mv tmp $i
> end
> 
Try sed "s/foo/$i/" < $i > tmp

The single quotes in your command don't allow variable substitution.  The
shell ignores any otherwise special characters inside single quotes.

-- 
UUCP:		{rutgers|ames|uunet}!mimsy!woodb!fallst!tkevans
INTERNET:	tkevans%fallst@wb3ffv.ampr.org
Tim Evans	2201 Brookhaven Ct, Fallston, MD 21047  (301) 965-3286

cpcahil@virtech.uucp (Conor P. Cahill) (03/04/90)

In article <652@sagpd1.UUCP> jharkins@sagpd1.UUCP (Jim Harkins) writes:
>
>foreach i (list of files)
>	sed 's/foo/$i/' < $i > tmp
            ^^^^^^^^^^^
>	mv tmp $i
>end

The single quotes stop expansion of shell variables, hence the change to 
$i.  You should use something like

 	sed "s/foo/$i/" < $i > tmp
-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170