Help with VI script

mrmagoo_83

2[H]4U
Joined
Aug 8, 2002
Messages
3,068
Hi, I was wondering is there a way to write a script such that it VIs a file, replaces a given string then saves it?

The issue I am having is that I can get the script to VI and replace the set string, but I cannot figure out how to code the ESCAPE command. Because once I use the R command, I am stuck in Replace mode, and cannot get out.

I am very new to this type of thing, and the script is extremely simple.
Ex:
Code:
vi /etc/hosts
/PC1
RPC2

ZZ

And thats as far as it gets, well actually it does, it just adds ZZ to the PC2 so it appears PC2

ZZ
Instead of PC2 then saving and exiting.

Anyone help me out?
 
You'll be better off doing this with a short perl script or a one-line sed command. vi isn't really designed to be used quite in the way you're trying to use it.
 
I don't know what that means, I guy at work said the same thing :)

Is that you James :) lol.

Can you tell me how to do that, as I don't understand command line or really what that means. I mean I know what a command line is, but not how to program with it.
 
Check out perl.org and the sort. Google for perl howto. Just type "perl" at the command line and see what happens. Play with it. Read the docs. Learn.
 
Ok here is what I have come up with:

Code:
#!/bin/bash

echo "Location:";
read LOC

set $XLOC="ZZZZZ"

#ex /tmp/XXX.sql
#Location Replacement

sed "s/$XLOC/$LOC/g" /tmp/XXX.sql

But I get this out:
sed: -e expression #1, char 9: No previous regular expression

Any ideas what that means?
 
Correction, it some what works....

When i try to run multiple sed lines one after another, it kinda hoses up...will have to look into that.
 
I cannot do this

sed -e yata/yata/yata xxx.sql > xxx.sql

It blanks the file

I also cannot do this

sed -e yata/yata/yata xxx.sql > xxx1.sql
sed -e yata/yata/yata xxx1.sql > xxx2.sql


I have a ton of things i need to change but it doesn't seem to like me trying to change them all at once, wonder how I am going to do this. oh maybe if I run the first sed command then move the file from the 1.sql back to the xxx.sql.
 
Nice, I might be on to something there, but its time to go before I get in trouble for too much OT.
 
sed --help

Code:
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if extension supplied)
 
Is there a sed command to enter a blank line? I cannot seem to find anyway to add a blank line.
 
you will not be able to > /file after a sed command because sed produces no output to the terminal except for errors.
if you want more (of my probably feeble attempts to) help, maybe you could describe exactly what you are trying to do with this script.
 
mrmagoo_83 said:
Is there a sed command to enter a blank line? I cannot seem to find anyway to add a blank line.

Does adding the string "\n" work?
 
naw i figured it out.

New question, thanks for helping guys.

I do the following:
sevice cluster status > scs.txt
grep running scs.txt | wc -1 > scs.txt

Ok bascilly that takes the running status of the files, writes them to the file, then tells me how many are running. The scs.txt file winds up containing the number 7, or so it should. What I need to know is how do i pull that number out into a string so I can compare it to 7 and see if there are actually 7 processes running?
 
you could just add an
if [ `echo scs.txt` -e '7' ]
then
echo "its equal to 7 mrmagoo!\n' (or is the \n outside of the quotes...?)
else
echo "sorry its equal to `echo scs.txt` instead\n"
fi
 
Do you really need the txt file for later use? If not, just try a


if [ `service cluster status | grep running | wc -l` -e 7 ]


Or, if you want to use it in multiple places

Code:
#!/bin/bash

SCS = `service cluster status | grep running | wc -l `

if [ $SCS -e 7 ]
   echo "$SCS is 7, right on!"


....
 
You might wanna read a guide on Bash scripting if you plan on doing too much more...

there's always the "Advanced Bash-Scripting Guide" over at http://www.tldp.org

This document is both a tutorial and a reference on shell scripting with Bash. It assumes no previous knowledge of scripting or programming, but progresses rapidly toward an intermediate/advanced level of instruction. The exercises and heavily-commented examples invite active reader participation. Still, it is a work in progress. The intention is to add much supplementary material in future updates to this document, as it evolves into a comprehensive book that matches or surpasses any of the shell scripting manuals in print.
 
Thanks, i will look at that. My wife and are should be heading to Barnes and Noble tonight. She hates me going there, I already have all those stupid teach yourself yata yata in so long for a ton of stuff, php, unix, dreamweaver, xml, apache; some of them were ok, but I am sure glad I bought them all onsale. I need to get a real book this time.
 
Back
Top