Unix、コマンドに基づいてファイルを生成 [重複]

Unix、コマンドに基づいてファイルを生成 [重複]

コマンドがありますが、結果を開くことができる.txtファイルに保存したいと思います。結果をテキストファイルに入れるようにコマンドをどのように変更できますか?この.txtファイルをローカルデスクトップに転送する予定です。

my command | grep stackoverflow 

私が試したこと: echo my command | grep stackoverflow > ex.txt しかし、.txtファイルには何も表示されません。

ありがとうございます。

ベストアンサー1

まあ、デフォルトでは出力リダイレクトを使って

my command|grep stackoverflow > file       #writes output to <file>
my command|grep stackoverflow >> file      #appends <file> with output
my command|grep stackoverflow|tee file     #writes output to <file> and still prints to stdout
my command|grep stackoverflow|tee -a file  #appends <file> with output and still prints to stdout

パイプはstdoutのすべてを取得し、後続のコマンドへの入力として使用します。だから:

echo "this is a text" # prints "this is a text"
ls                    # prints the contents of the current directory

grepは入力で一致する正規表現を見つけようとします。

echo "my command" | grep stackoverflow  #will find no matching line.
echo "my command" | grep command        #will find a matching line.

「my command」は、「my command」というメッセージではなく、コマンドを意味するようです。

おすすめ記事