How to run VI in batch mode


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to run VI in batch mode
# 1  
Old 04-03-2010
How to run VI in batch mode

Hi how do I use vi to do change some strings in a shell script loop

1. Run ls first, for each file that contains the word salesreport*.txt, do the following
2. use vi to run the following ex command : "1,$s/1975/1945/ig, wq"


Please tell me how to do this in vi, not sed. Thank you.
# 2  
Old 04-03-2010
Hi.

The vi editor is another aspect of ex, so just use ex

You'll need to explicitly write the file, as well as exit, all with ex commands -- the wq should suffice for that.

When I use ed / ex, I usually use a here document for the commands:
Code:
ex file <<'EOF'
ex-command1
ex-command2
...
wq
EOF

... cheers, drl

Last edited by drl; 04-03-2010 at 07:13 PM.. Reason: Edit 1: quotes around EOF to avoid interpretation of h.d. content
# 3  
Old 04-04-2010
Thanks,
may I ask what is the /bin/sh procedure for doing a loop on the output of ls:

imaginary code:
for filehandle in (ls salesreport*.txt):
do
ex filehandle <<'EOF'
1,$s/1975/1945/ig
wq
EOF
end
# 4  
Old 04-04-2010
This is how you have to use the for loop..
Code:
for filehandle in `ls salesreport*.txt`
do
echo "->$filehandle<-"
done

# 5  
Old 04-04-2010
Quote:
Originally Posted by grossgermany
Code:
for filehandle in (ls salesreport*.txt)

Quote:
Originally Posted by thegeek
This is how you have to use the for loop..
Code:
for filehandle in `ls salesreport*.txt`

Both of those for loops are broken. They will fail to properly handle filenames with IFS characters (by default a space, tab, or newline), and will fail if the result of the filename expansion exceeds the system's command line length limit (when attempting to execute the ls).

The simplest, most efficient, and safest way to iterate over the filenames matching that pattern is:
Code:
for filehandle in salesreport*.txt

Simple demonstration:
Code:
$ touch 'with space1' 'with space2'

$ # Broken code
$ for f in $(ls w*); do echo "$f"; done
with
space1
with
space2

$ # Correct code
$ for f in w*; do echo "$f"; done
with space1
with space2

Regards,
Alister

Last edited by alister; 04-04-2010 at 08:59 AM..
# 6  
Old 04-04-2010
Hi, grossgermany.
Quote:
Originally Posted by grossgermany
Thanks ...
You are welcome.

Meta-suggestions:

If someone is searching the forum for guidance on for loops, they might easily miss alister's excellent advice posted here because the title of the thread is How to run VI in batch mode, not about How to use loops. That's why it is best to start a new thread with an appropriate title for a new question. If necessary, you can include a reference to the old thread URL from the new thread.

Secondly, it is much easier to read code and data in threads when the text is surrounded by CODE tags. To do that almost effortlessly just select the text, and click the # symbol above the forum editing window. The result will be
Code:
like this

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Test if Remote server is up and running before SFTP'ing files (in batch mode)

Hello, In our Data Warehouse environment, before our batch SFTP jobs kick off to pull the files from remote servers, I would like to setup a pre-sftp job that would test if all the remote servers from where the files are being pulled, are up and running. If any one of the remote serer is... (2 Replies)
Discussion started by: Dippu
2 Replies

2. Shell Programming and Scripting

How to use gpg to encrypt data in batch/non-interactive mode?

Hello, I have to encrypt data using a script in batch mode without being prompted. I can successfully encrypt data but it prompts me to enter (y/N) as shown below. I tried batch flags without no success. I need to automate this without any prompts. Appreciate your inputs. Thanks, C. $... (2 Replies)
Discussion started by: calredd
2 Replies

3. Shell Programming and Scripting

tftp batch mode within bash script

Hi, I put the necessary tftp commands into a batch file and I can run tftp by $ tftp < tftpbatchscript in bash command line and then successfully exit. Now, I want to put a line which does the same thing above. However, when I put this line into a bash script, the lines below this line... (1 Reply)
Discussion started by: yildiz.a
1 Replies

4. Shell Programming and Scripting

FTP in batch mode

HI, Need to ftp a bunch of files in a directory in batch mode. TRying to ftp a single file first with below code. #!/bin/ksh function ftp_files { ftp -n ${D2_SRVR} <<-EOF quote user ${D2_UID} quote pass ${D2_PWD} cd ${D2_DIR}/${D2_NAME} lcd ${D1_DIR}/${D1_NAME}/dml/ put file1 ... (6 Replies)
Discussion started by: cvsanthosh
6 Replies

5. HP-UX

how to run glance over ssh in batch mode

Hello; Is it possible to run glance over ssh in batch mode ?? Similar to running " top -f " command over ssh.. Need to get glance output for specific pids Thnx very much (5 Replies)
Discussion started by: delphys
5 Replies

6. Gentoo

top in batch mode, cpu info is wrong

well. the title says it all. im runing top in batch mode like this top -b -n1 > somefile but the cpu usage info is not correct. if i run top normally, the first second, i see the same wrong info, and then it corrects itself. i found only one small mention of it on this forum. with this link... (7 Replies)
Discussion started by: broli
7 Replies

7. UNIX for Advanced & Expert Users

Sftp in Batch Mode

Hi, I am trying to do sftp a file from one server to another solaris server. Both are sftp enabled. I have generated the rsa key in local server and did a ftped the public key to the remote server and added that in the authorization keys file. Then i try to run the below command using a... (2 Replies)
Discussion started by: sivaemn
2 Replies

8. Shell Programming and Scripting

SSH in batch mode and File-Handles in a loop

Hi all I try to execute SSH commands on several hosts in a while-loop. There seems to be a problem with file-handle, first cycle works correct but no other one will follow due to the while condition is false even that there are many more host entries (lines) in all_hosts.dat. ... (3 Replies)
Discussion started by: DaveCutler
3 Replies

9. Shell Programming and Scripting

su command in batch mode

Hi, how do we change user in a shell script- batch mode. Thanks, Rajesh (3 Replies)
Discussion started by: Rajesh Gohad
3 Replies

10. UNIX for Dummies Questions & Answers

Help ! How to get elm to send multiple attachments - in batch mode

From a program, I want to execute a UNIX elm command that will send multiple txt attachment files to an email address. I can do it for one attachment only ie. "elm -s"subject" emailaddress < attachment.txt" 1. The attachment is received in the body of the email and not as an attachment. 2. The... (1 Reply)
Discussion started by: anarvan
1 Replies
Login or Register to Ask a Question