2

I want to search for a string in files and pipe the resulting file list to OSX's "open" command, thus opening all files that contain said string for editing in their default editor (aka not vim).

For example, running:

find . -name "*.txt" | xargs grep -l "RANDOMSTRING"

might return:

./folder1/file1.txt
./folder1/file2.txt
./folder2/file3.txt
./file4.txt

however, simply piping this list to the open command fails:

find . -name "*.txt" | xargs grep -l "RANDOMSTRING" | open

I think it's because of the "newlines" or "carriage returns" because running:

open ./folder1/file1.txt ./folder1/file2.txt ./folder2/file3.txt ./file4.txt

works.

I'm just not sure how to remove the newline characters from the returned output and pipe it to the open command. I need another command to pipe the output through that will format the file list properly to then pipe the list to the open command. Although the newline characters may not even be the problem.

P.S. - I DON'T want to create a temp file to do this. I am however open to creating a .zshrc or .bashrc function.

4

1 に答える 1

3

find . -name "*.txt" | xargs grep -l "RANDOMSTRING" | xargs open

openコマンドopenは入力が標準入力パイプからではなく、適切な形式であると想定していたため、保持のみが機能しませんでした。xargsstdin から 1 行ずつ読み取り、にフィードしopenます。また、補足として、xargsスペースを含むファイル名で問題が発生することがあります。したがって、find では-print0、最後にオプションを使用してそれに対処します。xargs を使用した検索のサンプルは、man xargs

于 2013-05-14T07:01:50.647 に答える