views:

3681

answers:

3

I'm writing a shell script to edit Change-Set attributes of aegis. The command I'm using is:

aegis -Change_Attributes -Edit

which opens up a vi editor to carry out the changes. I want to do a search and replace:

s/brief_description \= \"none\"\;/brief_description \=

\"test\"/g

Can I pass these directly to the open vi instance via the script without typing in any of it? I want to save the document (:wq) after editing it.

P.S. The file is a temporary file created when executing the command so I don't know the original path

Edit: I could used sed in this case:

sed -e 's/brief_description\ \=\ \"none\"\;/brief_description\ \=\

\"test\"\;/g'

The solution (inelegant hack??) would be to "cat" the output from aegis (setenv VISUAL cat), modify the out put stream with the above command and save it to a temp file, and use :

aegis -change_attributes -file <temp file>

EDIT2: I've almost got it to work. But there's a problem with the way I use "sed"

I have the following line in my script:

sed -i 's/brief_description\ \=\ \"none\"\;/brief_description\ \=\ \"${DESC}\"\;/g' temp_next.txt

But the $DESC variable does not evaluate to its value and the out put is given as:

brief_description = "${DESC}";

How can I pass DESC to sed that it would evaluate to it's actual value?

EDIT3:

Using

sed -i 's%brief_description\ \=\ \"none\"\;%brief_description\ \=\ \"'"$DESC"'\"\;%g' temp_next.txt

worked. I replace the normal delimiter (/) with % and put the environment variable in double quotes.

+4  A: 

You don't need to know the path - the aegis app will supply that. You need to change the environment variable that specifies what editor aegis uses to point at a script, and in that script use the sed stream editor to perform your edits.

Edit: Regarding your variable name expansion problem, change the set of single quotes enclosing the whole sed substitution expression to double quotes. Variable substitution is turned off by single quotes.

anon
If I use cat and modify the output with sed, would the changes get persisted to the file?
Gayan
No, you have to redirect the output of sed somewhere, in this case to a temporary file. Then when the edit is done, delete the original and rename the temporary.
anon
If you use GNU sed, you can use the -i flag to edit the file in place.
skoob
When I use double quotes://sed -i "s/brief_description\ \=\ \"none\"\;/$DESC/g"I get an error saying //Unmatched ".// I tried several other approaches://sed -i 's/brief_description\ \=\ \"none\"\;/'"$DESC'"/g'////sed -i 's%brief_description\ \=\ \"none\"\;%'"$DESC'"%g'//but none of these seem to work. I'm working with csh not bash so this might be a problem with that
Gayan
csh was never designed fpr writing scripts with - if you haven't got bash, use sh instead
anon
A: 

I don't think that vanilla vi will do this, although vim might be able to this under control of a custom .vimrc. A better way would be to do the changes with sed and then open vi on the result.

ConcernedOfTunbridgeWells
+2  A: 

If you pass the desired command in via the -c option then vi will execute the command immediately after starting the edit session, e.g.

vi -c 's/brief_description \= \"none\"\;/brief_description \= \"test\"/g' my_file

Oops. I forgot to say that the command is interpreted as an "ex" command, i.e. at the colon, so the command you've provided should work.

HTH

cheers,

Rob Wells
Yes. That was what I was looking for. Unfortunately I don't open the vi session so I can't pass any command line options. I've resorted to using cat instead
Gayan
If you really want to use vi, you can do it - just use it instead of sed in the script I suggested. I think the use of sed is better though - I don't see it as inellegant.
anon