Operating System - HP-UX
1832936 Members
3238 Online
110048 Solutions
New Discussion

Doubt on vi in non-interactive mode

 
SOLVED
Go to solution
Ramsunder S
Occasional Advisor

Doubt on vi in non-interactive mode

Hi,
I was trying to edit a file using vi through a shell script.

#!/bin/ksh

vi - 1.txt <d$
i
New text
:wq
EOF

In the above script, after I do the insert, I need to return to the command mode. How do I enter the escape key in the script file to come back to the command mode?

Thanks.
Regards,
Ram.
9 REPLIES 9
Rodney Hills
Honored Contributor

Re: Doubt on vi in non-interactive mode

If your follow "new text" with a line that only has a "." in it, that turns off insert mode.

-- Rod Hills
There be dragons...
Shahul
Esteemed Contributor

Re: Doubt on vi in non-interactive mode

Hi

Why don't U use sed command for non interactive edition..It is very simple. I will give the syntax here

/line address/itext

eg: Suppose U have line like this

" HPUNIX is the best Unix" and U want to insert a line like this "HPUNIX is a 64 bit UX" Then script should be like this

/HPUNIX/iHPUNIX is a 64 bit ux

Best of luck
Shahul
Ramsunder S
Occasional Advisor

Re: Doubt on vi in non-interactive mode

Hi Rod,
I tried that, but I'm afraid it didn't work for me. I'm on HP-UX 11. Could it have something to with that?

Regards,
Ram.
Rodney Hills
Honored Contributor

Re: Doubt on vi in non-interactive mode

In that case I'm with shahul, "sed" is more appropriate for text manipulation. Although "perl" is better :-).

-- Rod Hills
There be dragons...
Ramsunder S
Occasional Advisor

Re: Doubt on vi in non-interactive mode

Shahul, Rod,
Thank you for your replies.
Actually, I'm not sure if I could use sed in my situation. It is like this. I'm actually running a script (which I cannot change) which needs to read in some comments half way down its processing. To read in the comments, it opens a temp file in vi. This file already has one line of text and I need to erase the contents and add my own text. I was trying to run this standard script from my own shell script (something like a batch script) which would invoke the standard script with different parameters multiple times. Would I be able to handle this using sed?

Regards,
Ram
Rodney Hills
Honored Contributor

Re: Doubt on vi in non-interactive mode

When you say "read in comments", I assume you jump into "vi" to allow a user to input some text (much like text mode mail programs).

If that is the case and you just want one line in the file prior to the user who enters in the comment, then do the following in your script.

cat - <1.txt
yourtext
EOD
vi 1.txt

That way the user of your script can begin adding comments and "yourtext" will be the first line.

-- Rod Hills
There be dragons...
Ramsunder S
Occasional Advisor

Re: Doubt on vi in non-interactive mode

I guess that should work fine.

Thanks.
Regards,
Ram.
harry d brown jr
Honored Contributor
Solution

Re: Doubt on vi in non-interactive mode

Ram,

USE "ed"

# cat 1.txt
this is line 1
this is line 2
this is line 3
this is line 4
# cat ttt
#!/usr/bin/ksh
ed - 1.txt <1
d
.i
New text
.
w
q
EOF
# ./ttt
this is line 1
# cat 1.txt
New text
this is line 2
this is line 3
this is line 4
#

live free or die
harry
Live Free or Die
Steve Steel
Honored Contributor

Re: Doubt on vi in non-interactive mode

Hi

ex (line mode vi)

Would be best


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)