Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何从S3中grep一个术语并输出对象名_Bash_Amazon S3_Grep_Command Line Interface_Aws Cli - Fatal编程技术网

Bash 如何从S3中grep一个术语并输出对象名

Bash 如何从S3中grep一个术语并输出对象名,bash,amazon-s3,grep,command-line-interface,aws-cli,Bash,Amazon S3,Grep,Command Line Interface,Aws Cli,我需要在S3中对数千个文件进行grep,并在一些输出文件中列出这些文件名。我对使用cli非常陌生,所以我已经在本地和s3的一小部分中进行了测试 到目前为止,我得到了这个: aws s3 cp s3://mybucket/path/to/file.csv - | grep -iln searchterm > output.txt 问题在于连字符。由于我正在复制到标准输出,grep中的-l开关返回标准输入,而不是file.csv 我期望的输出是 file.csv 最终,我需要在整个buck

我需要在S3中对数千个文件进行grep,并在一些输出文件中列出这些文件名。我对使用cli非常陌生,所以我已经在本地和s3的一小部分中进行了测试

到目前为止,我得到了这个:

aws s3 cp s3://mybucket/path/to/file.csv - | grep -iln searchterm > output.txt
问题在于连字符。由于我正在复制到标准输出,grep中的-l开关返回标准输入,而不是file.csv

我期望的输出是

file.csv
最终,我需要在整个bucket上迭代这个,然后在所有bucket上迭代,以获得

file1.csv
file2.csv
file3.csv
但我需要先克服这个障碍。
谢谢

因为您在STDOUT中打印文件并将其传输到grep STDIN,grep不知道原始文件是file.csv。如果您有一长串文件,我会:

while read -r file; do aws s3 cp s3://mybucket/path/to/${file} - | grep -q searchterm && { echo ${file} >> output.txt; }; done < files_list.txt
解释 步骤1和2相同,则:

stdout被重定向到sed,sed将逐行查找文件,直到找到第一个流模式,然后退出q,在输出文件中打印文件名F。
谢谢你能详细解释一下为什么每个元素都能起作用吗?完成了,我希望我的解释能对你有所帮助!我还添加了最近使用sed学习的另一个解决方案。
while read -r file; do aws s3 cp s3://mybucket/path/to/${file} - | sed -n /searchpattern/{F;q} >> output.txt; done < files_list.txt