ksh script to edit a file using vi editor


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh script to edit a file using vi editor
# 1  
Old 11-28-2014
Linux ksh script to edit a file using vi editor

I was wondering if it is possible to execute a script that will remove a certain search pattern from a file and save it?

Manually I would just hit escape to enter command mode then search and delete the pattern "./srv 135.0.0.1.11111 210;=1" then save & exit the file

vi command to search and delete indicated pattern:
:%s!"./srv 135.0.0.1.11111 210;=1"!!g


I dont have a clue how to do this using a script. Any ideas? Smilie
# 2  
Old 11-28-2014
Not sure I understand your problem. From the command line, you can use sed:
Code:
sed 's!"./srv 135.0.0.1.11111 210;=1"!!g' file1 > file2

# 3  
Old 11-28-2014
Quote:
Originally Posted by seekryts15
I was wondering if it is possible to execute a script that will remove a certain search pattern from a file and save it?

Manually I would just hit escape to enter command mode then search and delete the pattern "./srv 135.0.0.1.11111 210;=1" then save & exit the file

vi command to search and delete indicated pattern:
:%s!"./srv 135.0.0.1.11111 210;=1"!!g


I dont have a clue how to do this using a script. Any ideas? Smilie
Any vi command that starts with a : is really an ex command. Any time the shell and vi command sequence:
Code:
vi file
:ex command...
:w
:q

will make the changes you want to make to file, you can put it into a script as:
Code:
ex file <<"EOF"
ex command...
w
q
EOF

so in this case:
Code:
ex file <<"EOF"
%s!"./srv 135.0.0.1.11111 210;=1"!!g
w
q
EOF

should do what you want.
# 4  
Old 11-28-2014
Thanks Don! That worked perfectly!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

File edit script exceptions how to pass to script

i wrote script for if file exists script do file edit problem with the script get stuck not existing as for exit i mentioned exit 0 , and how to give the exception for script it should add ./script -- add hi ./script --add "hi how are you" now below script with case it is working for... (0 Replies)
Discussion started by: markjohn1
0 Replies

2. Shell Programming and Scripting

ksh script trying to pass a variable to edit a file

I'm trying to create a ksh script that will ask the user for the port number. $PORT1 is the variable I want to use that will contain whatever numbers the user inputs. The script would edit ports.txt file, search and delete "./serv 110.1.0.1.$PORT1 200;=3" . So if the user types 50243 then the... (5 Replies)
Discussion started by: seekryts15
5 Replies

3. Shell Programming and Scripting

Script to edit a text file

hi, could someone share a short script that would process a .txt file and do the following, for example the text file has this form 0:1.0 1:1.0 2:2.0 3:3.0 4:4.0 5:5.0 6:6.0 7:7.0 8:8.0 ... {newline} 9:9.0 10:10.0 11:11.0 12:12.0 13:13.0 14:14.0 15:15.0 16:16.0 17:17.0 ... {newline} and I... (3 Replies)
Discussion started by: c_lady
3 Replies

4. Shell Programming and Scripting

shell script to edit a file

i have a file called number which contains data as 1 2 3 4 5 6 7 8 9 0 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 needed a shell script to print the output as 1 7 7 1 4 and (2 Replies)
Discussion started by: jacky29
2 Replies

5. Solaris

edit a file without use editor vi

hi all i have a problem i want to edit a file without use the "vi" . for example i want to edit a passwd file without to do "vi" in file. how i can to do this? Regards (4 Replies)
Discussion started by: FrancescoIt
4 Replies

6. Shell Programming and Scripting

Need help in writing a script to edit a file

Hi all, I need help in writing a script to edit a file Here is the sample of my file abc xxx 123 456 789 045 def yyy 987 678 098 cdf zzz 435 543 jhg vvv 987 765 (2 Replies)
Discussion started by: leo.maveriick
2 Replies

7. Shell Programming and Scripting

KSH script to run other ksh scripts and output it to a file and/or email

Hi I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email. For example ------- Script ABC ------- a.ksh b.ksh c.ksh I need to call all three scripts execute them and... (2 Replies)
Discussion started by: pacifican
2 Replies

8. Shell Programming and Scripting

edit a file using ksh

Hi, sample file looks like this.. <hp> <name> <detail>adsg</detail> ... ... </name><ft>4264</ft> </hp> I need to edit the last but one line. I want the format to be .. <hp> <name> <detail>adsg</detail> ... ... </name> (3 Replies)
Discussion started by: meghana
3 Replies

9. Shell Programming and Scripting

How to EDIT file using VI in a script

Hello, How would i go about editing a file using VI within a shell script please? Basically, i want to open a file, clear it's contents and save the file. I found this on the web using "ex" but can't seem to get it to work: ex /home/oconnor/TOOLS/UNIXCMDS/test_ex <<eof_ex dd /*i put... (2 Replies)
Discussion started by: bobk544
2 Replies

10. Shell Programming and Scripting

Edit txt file using vi editor

Dear All I have a file called sample.txt which contains as follows HR Files records Loaded RecordDate Unloaded -- ---- -------- --------- ------------ ------ 00 567 77777 67896 0 0 01 345 345567 45678 0 ... (1 Reply)
Discussion started by: tkbharani
1 Replies
Login or Register to Ask a Question