grepはsed出力を改行区切りリストとして扱いません。

grepはsed出力を改行区切りリストとして扱いません。

今日遊んで出力を消費するためにgrepを取得しようとしました...

$(apt-cache rdepends hunspell-fr | sed -e 's/^\s*|\?//' -e 1s/^/\'/g -e \$s/$/\'/)

...単一引用符で囲まれたパターンの改行で区切られたリストです。上記のコマンドを実行してから、結果の出力を新しいgrepコマンドにコピー/貼り付けると、期待どおりに機能します。

nohatsatthetable@debian:~$ dpkg -l | grep 'hunspell-fr
Reverse Depends:
hunspell-fr-classical
thunderbird-l10n-fr
firefox-esr-l10n-fr
thunderbird-l10n-fr
task-french-desktop
hunspell-fr-revised
hunspell-fr-revised
hunspell-fr-comprehensive
hunspell-fr-comprehensive
hunspell-fr-classical
firefox-esr-l10n-fr'

ii  firefox-esr-l10n-fr                    102.4.0esr-1~deb11u1             all          French language package for Firefox ESR
ii  hunspell-fr                            1:7.0-1                          all          French dictionary for hunspell (dependency package)
ii  hunspell-fr-classical                  1:7.0-1                          all          French dictionary for hunspell (classical version)
ii  task-french-desktop                    3.68+deb11u1                     all          French desktop

しかし、次の1行のコード(私の考えでは機能的に同じでなければなりません)を実行すると、grepは最初の行だけをパターンとして解釈し、すべての後続の行(または2行目の場合は単語)ディレクトリまたはパターンを検索するファイルです。

nohatsatthetable@debian:~$ dpkg -l | grep $(apt-cache rdepends hunspell-fr | sed -e 's/^\s*|\?//' -e 1s/^/\'/g -e \$s/$/\'/)
grep: Reverse: No such file or directory
grep: Depends:: No such file or directory
grep: hunspell-fr-classical: No such file or directory
grep: thunderbird-l10n-fr: No such file or directory
grep: firefox-esr-l10n-fr: No such file or directory
grep: thunderbird-l10n-fr: No such file or directory
grep: task-french-desktop: No such file or directory
grep: hunspell-fr-revised: No such file or directory
grep: hunspell-fr-revised: No such file or directory
grep: hunspell-fr-comprehensive: No such file or directory
grep: hunspell-fr-comprehensive: No such file or directory
grep: hunspell-fr-classical: No such file or directory
grep: firefox-esr-l10n-fr': No such file or directory

なぜこれですか?

ベストアンサー1

はい、@言ったように先行は達成するのが難しい 、引用符は必須です。

$ dpkg -l |
    grep "$(apt-cache rdepends hunspell-fr |
        sed -e 's/^\s*|\?//' -e 1s/^/\'/g -e \$s/$/\'/
 )"                   

おすすめ記事