LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 07-02-2013, 10:12 AM   #1
goillini
LQ Newbie
 
Registered: Nov 2007
Distribution: centOS
Posts: 16

Rep: Reputation: 0
execute VI command from bash script


Goal:
Execute a vi find-and-replace command and continue running bash script.

This is as close as I've gotten:
Code:
vi -e -c %s/DOMAINNAME/example.com/g template.conf
This goes trough template.conf and replaces every instance of "DOMAINNAME" with "example.com" the problem is now I still need an additional command to save the file. How can I edit and save from a bash script?

Thanks!
-Adam

Last edited by goillini; 07-02-2013 at 02:15 PM. Reason: solved
 
Old 07-02-2013, 10:31 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,678

Rep: Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971
Quote:
Originally Posted by goillini View Post
Goal:
Execute a vi find-and-replace command and continue running bash script.

This is as close as I've gotten:
Code:
vi -e -c %s/DOMAINNAME/example.com/g template.conf
This goes trough template.conf and replaces every instance of "DOMAINNAME" with "example.com" the problem is now I still need an additional command to save the file. How can I edit and save from a bash script?
Why use vi? Sed (the stream editor), does exactly what you're after. So:
Code:
sed -i 's/DOMAINNAME/example.com/g' <filename>
does it.
 
1 members found this post helpful.
Old 07-02-2013, 02:14 PM   #3
goillini
LQ Newbie
 
Registered: Nov 2007
Distribution: centOS
Posts: 16

Original Poster
Rep: Reputation: 0
Beautiful!

Thanks,
-Adam
 
Old 07-03-2013, 02:45 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
There's nothing wrong with using vi/vim for this. In fact, the gnu version of the ex editing command is actually equivalent to "vim -es", which I think is probably easier to use than vim -ec. You can simply feed it any sequence of ex-mode commands from stdin that you want, without having to worry about special formatting or escaping or whatever.

In any case, you just need to add a "write" command to the end of the sequence to save it back to file, just as you would in interactive vi.

Code:
printf '%s\n' '%s/Foo/Bar/g' 'w' | ex file.txt
A similar, but lighter, command is ed. The basic syntax for most actions is pretty much identical to ex (although it doesn't have anywhere near the range and flexibility).

Code:
printf '%s\n' '%s/Foo/Bar/g' 'w' | ed -s file.txt
How to use ed:
http://wiki.bash-hackers.org/howto/edit-ed
http://snap.nlc.dcccd.edu/learn/nlc/ed.html
(also read the info page)


However, as mentioned by TB0ne, the best option for most simple edits is generally sed. sed works pipeline-style and simply runs through the file once, one line at a time, from top to bottom. So as long as the edits don't cross line boundaries, it's usually easier and more efficient.

Here are a few useful sed references:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt
http://www.catonmat.net/series/sed-one-liners-explained

vim and ed, OTOH, load the whole file into a memory buffer before editing, which makes them a bit slower, but does also allow you to operate on the whole file in random-access fashion. This makes multi-line work in particular much easier than with sed. Plus, you can easily save the buffer directly back to file, either the original or a new one.

(gnu sed also offers the -i option for saving back to the original file, but note that this is actually just using a tempfile behind the scenes to do so.)

Last edited by David the H.; 07-03-2013 at 02:48 PM. Reason: +linkies
 
1 members found this post helpful.
  


Reply

Tags
bash, vi



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute bash script in C waqasdaar Programming 15 10-27-2015 02:54 PM
Can't execute Bash script. zbe Linux - Software 4 10-17-2008 08:05 AM
Execute command with spaces from variable in bash script klo_2k Linux - Newbie 4 04-13-2008 02:59 AM
Bash: execute command Ephracis Programming 9 10-07-2007 10:58 AM
[Bash] execute string as command michael_hk Linux - Newbie 2 06-26-2006 03:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 10:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration