home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!olivea!decwrl!concert!sas!mozart.unx.sas.com!sasegb
- From: sasegb@toklas.unx.sas.com (Elizabeth Brownrigg)
- Newsgroups: comp.unix.shell
- Subject: Re: Sed with shell variables
- Message-ID: <BtLHtK.6IB@unx.sas.com>
- Date: 26 Aug 92 14:34:31 GMT
- References: <2701@nlsun1.oracle.nl>
- Sender: news@unx.sas.com (Noter of Newsworthy Events)
- Organization: SAS Institute Inc.
- Lines: 52
- Originator: sasegb@toklas.unx.sas.com
- Nntp-Posting-Host: toklas.unx.sas.com
-
-
- In article <2701@nlsun1.oracle.nl>, rgasch@nl.oracle.com (Robert Gasch) writes:
- |> Using sed to edit strings is usually a nice and practical
- |> method of doing so. I you want to change a pattern you usually
- |> say:
- |> echo $something|sed 's/pattern1/pattern2/'
- |>
- |> The problem I ran into is that I can't get shell variables to
- |> work as arguments. When I type
- |> echo $something|sed 's/$this_pattern_var/$that_pattern_var/'
- |>
- |> sed throws up on me. I've tried various combinations of quotation
- |> marks without success. Is there an easy way of doing this??
- |>
- |> Thanks a lot
- |> ---> Rob
-
-
- Here's a script I wrote using sed and shell variables to globally
- search and replace strings in all the files in a subdirectory:
-
-
- echo "Enter the search string. Precede regular expressions with
- a double backslash."
-
- read firststring
-
- echo "Enter the replacement string. Precede regular expressions
- with a double backslash."
-
- read secondstring
-
- for file in *
-
- do
- echo 'Starting file '$file
-
- # Use the sed editor to search and replace firststring with
- # secondstring in each file in the subdirectory. Write the corrected
- # file to file.sed.
- sed "s/$firststring/$secondstring/g" $file >$file.sed
-
- rm $file
- mv $file.sed $file
-
- echo 'Ending file '$file
-
- done
-
-
-
- - Elizabeth
-