我正在尋找一個(bash)命令來打開一個檔案(比如在 vim 中),搜索一個特定的字串,然后從那里向后搜索,找到第二個字串,并輸出(僅)第二個字串。對于這樣的檔案:
...
aaa x
...
aaa y
... <-- any number of lines, but necessarily greater than 0
bbb
...
到目前為止,我想出了這個:
vim -c "/bbb" -c "n" -c "?aaa" -c "n"
這不起作用;vim 寫道:
Error detected while processing command line:
E163: There is only one file to edit
Press ENTER or type command to continue
我還需要將字串保存aaa y到檔案/變數中——我不知道如何。
TIA
uj5u.com熱心網友回復:
為了解決您的主要問題……在-c引數中執行的命令是 Ex 命令,而不是普通模式命令。碰巧:/和:?是有效的 Ex 命令,并且像正常模式的對應物and一樣模糊地作業,但它是 的簡寫:/?:n:help :next
Edit [count] next file.
這與普通模式非常不同:help n。
但是你為什么要在 Vim 中這樣做呢?好像不太合適。
uj5u.com熱心網友回復:
正如其他人所說,vim可能不是您想要實作的最佳工具。
你可以試試 :
#!/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
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/365313.html
