user3144035 user3144035 - 1 year ago 86
Bash Question

Running vi within a bash script and executing vi commands to edit another file

So I've made a script which is collecting data from many different files:


mkdir DATAPOOL"$1"

grep achi *out>runner
grep treat *out>>runner

cat runner | grep Primitive *gout | grep '= '|awk '{print $1,$6}' > CellVolume"$1".txt
cat runner | grep ' c ' *gout | grep 'Angstrom '|awk '{print $1,$3}' > Cellc"$1".txt
cat runner | grep 'Final energy ' *gout |awk '{print $1,$5}' > CellEnergy"$1".txt

etc etc

cat runner |awk '{print "~/xtlanal",$1," > ",$1}' >runner2
vi runner2

source runner2

grep Summary *dat | grep 'CAT-O ' |awk '{print $1,$6}' > AVE_NaO_"$1".txt

mv *txt DATAPOOL"$1"

So I end up with all the required text files when run without the vi part and so I know it all works. Furthermore when I run it with the vi commands, it just stops running at the vi command and then i can manually enter the 3 commands and I end up with the correct results. What I'm struggling with is I cant get vi to run the commands on its own so I can just execute the file multiple times within different directories and not have to manually enter commands time and time again.

Any help would be greatly appreciated.


Answer Source

For scripted editing tasks, you can use ed instead of vi:

ed runner2 <<'END'

For global line-oriented search and replace, sed is a good choice:

sed -i 's/gout:/xtl/; s/gout:/dat/' runner2
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download