私はbashを学んでいます。私は一度にgrepで戻り値とマッチした行を取得したいと思います。返り値を取得し、bashで一度にgrepで一致した行を得る方法はありますか?
if cat 'file' | grep 'match_word'; then
match_by_grep="$(cat 'file' | grep 'match_word')"
read a b <<< "${match_by_grep}"
fi
上記のコードでは、grepを2回使用しました。私は一度grepでそれをやる方法を考えることができません。 catはエラーメッセージを出力する可能性があるので、一致する単語がない場合でもmatch_by_grepは常に空です。
match_by_grep="$(cat 'file' | grep 'match_word')"
if [[ -n ${match_by_grep} ]]; then
# match_by_grep may be an error message by cat.
# So following a and b may have wrong value.
read a b <<< "${match_by_grep}"
fi
教えてください。どうもありがとうございました。
は、サンプル入力と期待出力 – Inian
ショー入力、期待される出力を提供します。 – 123
補足として、 'cat file | grep pattern file'ではなく 'grep pattern file'を使うのが望ましいです。 grep pattern' – Aaron