我想從程式中獲取輸出並互動式地過濾哪些行透過管道傳輸到下一個命令。
ls | interactive-filter | xargs rm
例如,我有一個模式無法匹配以減少的文件列表。我想要一個命令interactive-filter
來分頁文件列表的輸出,並且我可以互動式地指示將哪些行轉發到下一個命令。在這種情況下,每一行都會被刪除。
答案1
iselect
提供一個上下列表(作為來自前一個管道的輸入),使用者可以在其中標記多個條目(作為下一個管道的輸出):# show some available executables ending in '*sh*' to run through `whatis` find /bin /sbin /usr/bin -maxdepth 1 -type f -executable -name '*sh' | iselect -t "select some executables to run 'whatis' on..." -a -m | xargs -d '\n' -r whatis
按空白鍵在我的系統上標記一些後的輸出:
dash (1) - command interpreter (shell) ssh (1) - OpenSSH SSH client (remote login program) mosh (1) - mobile shell with roaming and intelligent local echo yash (1) - a POSIX-compliant command line shell
vipe
允許互動式編輯(使用自己喜歡的文字編輯器)通過管道的內容。例子:# take a list of executables with long names from `/bin`, edit that # list as needed with `mcedit`, and run `wc` on the output. find /bin -type f | grep '...............' | EDITOR=mcedit vipe | xargs wc
輸出(在 中刪除一些行後
mcedit
):378 2505 67608 /bin/ntfs-3g.secaudit 334 2250 105136 /bin/lowntfs-3g 67 952 27152 /bin/nc.traditional 126 877 47544 /bin/systemd-machine-id-setup 905 6584 247440 total
推拉注意事項:
iselect
從一個清單開始,其中沒有什麼被選中。vipe
從一個清單開始,其中每一個顯示的項目將透過管道發送,除非使用者刪除它。
在德班基於發行版,兩個實用程式都可以通過apt-get install moreutils iselect
.
答案2
vipe
寫幾行shell就可以了。對我有用的快速而簡單的概念驗證:
EDITOR=vi # change to preferred editor as needed.
vipe()
{
cat > .temp.$$
if $EDITOR .temp.$$ < /dev/tty > /dev/tty 2>&1 ; then
cat .temp.$$
fi
rm .temp.$$
}
將其放入您的 shell 中即可。其目的if
是在編輯器(或嘗試運行編輯器)失敗時抑制輸出的產生。