Computing.Net > Forums > Linux > use vi within script?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

use vi within script?

Reply to Message Icon

Name: numassa
Date: November 20, 2006 at 06:29:49 Pacific
OS: Linux Red Hat
CPU/Ram: 2/1
Product: Sony
Comment:

Hi, I'm very new to unix/linux and was wondering if you could edit a text file from within a script. e.g if I ran...
sh scriptname test

Then, the script would insert the word 'test' into the file at a predetermined point e.g after the first line.

Apologies if this is already covered somewhere I did search.
Thanks



Sponsored Link
Ads by Google

Response Number 1
Name: 3Dave
Date: November 20, 2006 at 10:09:10 Pacific
Reply:

Have a look at "sed" instead....


0

Response Number 2
Name: numassa
Date: November 20, 2006 at 11:18:31 Pacific
Reply:

ok wil do -thanks


0

Response Number 3
Name: nails
Date: November 20, 2006 at 11:33:06 Pacific
Reply:

For this particular problem, I like awk:

awk ' { if(NR == 2) print "test"; print $0 } ' myfile

Although it's not commonly done, you can call vi within a script using the unix "here" document:

#!/bin/ksh

text="test"
vi myfile << MSG
o$(printf "%s" "$text") $(echo '\033'):wq
MSG
# end script

All the keystrokes after vi'ing the file go within the here document - open the line below line 1, enter the string with printf, press the escape key, write and quit. I don't care for it myself.


0

Response Number 4
Name: Dlonra
Date: November 20, 2006 at 17:28:49 Pacific
Reply:

I have created simple to complex editing scripts using ex. I generally put the scripts into a file.
ex myfile < myscript

ex is preferred to vi because using vi in a script results in strange stuff on your terminal.


0

Response Number 5
Name: numassa
Date: November 21, 2006 at 03:40:13 Pacific
Reply:

Thanks to everyone that replied. I went with the 'sed' method but the other replys look interesting and I shall look at them further in the future. Thanks for all your help


0

Related Posts

See More



Response Number 6
Name: nails
Date: November 21, 2006 at 11:14:54 Pacific
Reply:

If vi's output is causing you problems, simply send it to /dev/null:

#!/bin/ksh

text="test"
vi myfile.txt > /dev/null << MSG
o$(printf "%s" "$text") $(echo '\033'):wq
MSG > garbage.txt


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Linux Forum Home


Sponsored links

Ads by Google


Results for: use vi within script?

How to use sed to remove ^M from files? www.computing.net/answers/linux/how-to-use-sed-to-remove-m-from-files/9335.html

Using color escape codes in less, www.computing.net/answers/linux/using-color-escape-codes-in-less-/30014.html

Scripting diffrence of two dates www.computing.net/answers/linux/scripting-diffrence-of-two-dates/27575.html