Script: How to substitute a pattern in a file

Script: How to substitute a pattern in a file

Post by nicolas Gro » Fri, 05 Mar 1999 04:00:00



I'm writing an installation script in sh, and would like to substitute a
pattern in a
configuration file.

What try to do is : opening the file with vi, substituing the pattern
"EXTRA_LIBS="
by "EXTRA_LIBS=-L/usr/local/lib/mysql -lmysqlclient -lm" with vi
commands, and closing the file.

#!/bin/sh
vi Configuration <<EOF
:^,$s/EXTRA_LIBS=/EXTRA_LIBS=-L\/usr\/local\/lib\/mysql -lmysqlclient
-lm/g
EOF

The substitute command works when executed manually but it doesn't work
in the script.

Any suggestions?

Thanks,

Nicolas Gros,
Development Engineer
Realmedia Europe

 
 
 

Script: How to substitute a pattern in a file

Post by Birger Blix » Fri, 05 Mar 1999 04:00:00



> I'm writing an installation script in sh, and would like to substitute a
> pattern in a
> configuration file.

> What try to do is : opening the file with vi, substituing the pattern
> "EXTRA_LIBS="
> by "EXTRA_LIBS=-L/usr/local/lib/mysql -lmysqlclient -lm" with vi
> commands, and closing the file.

> #!/bin/sh
> vi Configuration <<EOF
> :^,$s/EXTRA_LIBS=/EXTRA_LIBS=-L\/usr\/local\/lib\/mysql -lmysqlclient
> -lm/g
> EOF

> The substitute command works when executed manually but it doesn't work
> in the script.

> Any suggestions?

> Thanks,

> Nicolas Gros,
> Development Engineer
> Realmedia Europe

The shell is trying to translate all $,^  , so vi don't get it.
Use \* , \^ , or ' ' to prevent this.

vi Configuration <<'EOF'     (this will protect the input from the
shell)
                   ^   ^

/BB

  Birger.Blixt.vcf
< 1K Download

 
 
 

Script: How to substitute a pattern in a file

Post by Barry Margoli » Fri, 05 Mar 1999 04:00:00




>Pardon me for not addressing the vi issue, but why not use sed? Since this is
>invoked from within a script, you have no keystrokes to fuss with:

>cat Configuration | sed 's&EXTRA_LIBS=&EXTRA_LIBS=-L\/usr\/local\/lib\/mysql
>-lmysqlclient -lm&g' | (cat > Configuration)

It's not safe to redirect output to one of your input files on the same
command lie.  There's nothing preventing (cat > Configuration) from running
before or in the middle of "cat Configuration", so the latter may read an
empty or half-empty file.  You should write to a new file and then rename
it.

But I agree that vi usually shouldn't be used in scripts, since it's
intended for full-screen use.  Ex is vi without the full-screen UI, and ed
is even simpler and probably more efficient for anything that it supports.

BTW, that's a Useless Use of Cat.

--

GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

1. Substitute this pattern for that pattern EXCEPT if this is true with sed???

Hi!

I want to add debug macros to my C source files. I want the word TRACE
to show up after every '{' and ';' unless ';' is preceeded by 'return'
or 'int' or 'float' etc...

Here's what I got so far:

#!/bin/sh
find /home/epkolbl/workspace/rsd2/ -name '*.c' | while read file
do
        awk '{ print $0 }' $file | sed 's/;/;TRACE/g' | sed 's/{/{TRACE/g'
done

However this piece of script adds TRACE after ';' even though it is
preceeded by 'return' etc.

I have run out of ideas. Could someone please help me?

Thanks in advance
/Sune

2. printer (EPSON STYLUS COLOR 740)

3. substitute string for pattern in KSH 88

4. "Fingerprinting/Watermarking" server software

5. How sed-substitute a string that DOES NOT match my pattern...

6. Increase size of filesystems in AIX 4.X

7. Need fgres script (substituting from a file)

8. Full Duplex with CS 4232 Soundcards ?

9. substitute pattern

10. script to warn when pattern enters file?

11. Script that gives instance count unique patterns in a sorted file

12. Substituting :: for __ in every htm file, and rename htlml file

13. HOW using 1 files contents as info to delete the second files containing that pattern?