I'm looking for a (bash) command to open a file (like in vim), search for a specific string, and search backwards from there, to find a second string, and output (only) that second string. For a file like this:
...
aaa x
...
aaa y
... <-- any number of lines, but necessarily greater than 0
bbb
...
I've come up with this so far:
vim -c "/bbb" -c "n" -c "?aaa" -c "n"
This doesn't work; vim reads:
Error detected while processing command line:
E163: There is only one file to edit
Press ENTER or type command to continue
I also need to save the string aaa y
into a file/variable -- I can't figure out how.
TIA
CodePudding user response:
To address your main question… the commands executed in -c
parameters are Ex commands, not normal mode commands. It just so happens that :/
and :?
are valid Ex commands and work vaguely like their normal mode counterparts /
and ?
, but :n
is shorthand for :help :next
:
Edit [count] next file.
which is very different from normal mode :help n
.
But why are you doing that in Vim to begin with? It doesn't seem appropriate.
CodePudding user response:
As others said, vim
may not be the best tool for what you want to achieve.
You can try :
#!/bin/bash
expect <<EOF
spawn vim input-file
send ":1\r/bbb\rn\r?aaa\r:. 1,\$ d\r:1,.-1d\r:w! output-file\r"
sleep 1
EOF