ターミナル出力から特定のラインを取得する

ターミナル出力から特定のラインを取得する

「修正が必要なのにfile-i-must-edit-2どこにいるのか覚えていません。」

locate file-i-must-edit
/home/user/file-i-must-edit-1
/home/user/file-i-must-edit-2
/home/user/file-i-must-edit-3

「いいね!もう一度入力するのを避ける方法があればいいのに/home/user/file-i-must-edit-2…」

nano /home/user/file-i-must-edit-2 このように入力すると入力を避ける方法はありますか nano <output line 2>

答え1

1行の出力のみを取得するのは簡単です。

locate file-i-must-edit
nano $(!!)

より多くの行がある場合に使用できる技術がありますが、元のコマンドを別の方法で実行することが含まれます(常に実行したくはありません)。

$ touch a b c
$ OUT=( $(find .) )
$ echo ${OUT[2]}
./b

入力を避けるために行うことができる1つの方法は、前のコマンドを繰り返し(もちろんreadlineを使用して)スコープを絞り込んで1行だけ取得してから実行するnano $(!!)xargs

答え2

これはプログラム的なアプローチです(X-Windowsの選択にマウスを使用する場合と比較)。

# function to nano edit a line (by number) from $list

nano-n() { nano "$(sed -n "$1{p;q}" <<<"$list")"; }

# As you produce your list of files, save them to a variable ($list), 
#   as well as printng them to the terminal.
# Do this by "duplicating" descriptor 1 (stdout) to 
#  another available descriptor (eg. 9)

exec 9>&1; list="$(locate file-i-must-edit |tee /dev/fd/9)"

# now you can just type the following at the propmt (assuming that $list is available at the prompt)  

nano-n 2

関連情報