2

I would like to modify a particular line, say line 5, in a file by using the EOEX construct how can I achieve my aim? The code snippet that I use to achieve my purpose is as follows:

#!/bin/bash
S=0.030
F=0.150
N=30
DIFF=`echo "scale=3; $F - $S" | bc -l`
dw=`echo "scale=3; $DIFF / $N" | bc -l`
is=`echo "scale=3; $S / $dw" | bc -l`
if=`echo "scale=3; $F / $dw" | bc -l`
ex ~/Desktop/Dropbox/MATLAB/FFT.m  <<EOEX
  :5s/for*/for i = $S:$dw:$F/g
  :x
EOEX

I did some researh and learned that usually EOF(end of file) is used in here scripts for I/O redirection to the standard input of a particular command(in my case it is ex). I also learned that as long as the starting and finishing keywords are the same any word can be used instead of the EOF. My problem is that instead of the desired effect which is replacing the line with another for loop initializer in Matlab this script does not delete the line and append the string at the end of the fifth line. For reference purposes I have also added my FFT.m file. I apologize for the inconvenience caused by the abstractedness of my question previously. Note that I am also open to using another program to achieve the desired effect but curious about why it cannot be achieved in ex or vim. Please click here for the Matlab script.

7
  • If you work in VIM is much easy to do task via vim's macro or substitution commands. In any way you can invoke any bash command by twice ! on edited line in normal mode. – Costas Dec 24 '14 at 11:59
  • 1
    What's wrong with using sed? – Graeme Dec 24 '14 at 12:05
  • what bash script? What does vim have to do with it? What exactly is the EOEX construct? Please edit your question and explain what tools you are using and what exactly you want to do. – terdon Dec 24 '14 at 13:26
  • @terdon EOEX is just an acronym commonly used in here files for ex scripts instead of EOF. It apparently stands for 'end of ex' instead of 'end of file'. Not sure the OP really knows though... – Graeme Dec 24 '14 at 13:43
  • @terdon I edited my question you may wish to take another look. – Vesnog Dec 24 '14 at 16:58
6

To change line 5 of a file called test_file to 'hello world' using ex and the EOEX construct (by which I presume that what you mean is a 'here document'):

ex test_file << EOEX
5c
hello world
.
wq
EOEX

The above is also possible with ed, although note that POSIX ed doesn't support the wq command. Instead put the w and q on separate lines.

Using sed (a version that supports the -i option) is a bit simpler:

sed -i '15 c hello world' test_file
8
  • Thanks for the informative post yes I did mean here document by EOEX construct I was unfamiliar with terminology sorry before, sorry about that. – Vesnog Dec 24 '14 at 22:24
  • By the way does the . switch ex between insert and command mode? – Vesnog Dec 24 '14 at 22:37
  • Actually I was not able to pass the shell variable values and the exact string I wanted to the ex editor. – Vesnog Dec 24 '14 at 22:58
  • @Vesnog, no idea why the chose . to leave the insert mode. You are probably best posting a separate question about your shell variables and explaining what has gone wrong in detail there. – Graeme Dec 24 '14 at 23:12
  • @Graeme EDIT: The here document does not work when the lines following the output redirection are indented. Is this expected? – Vesnog Dec 24 '14 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.