简体   繁体   English

两个Onelinner For Cycle用于列表和grep Bash

[英]Two Onelinner For Cycle for list and grep Bash

I have a list of files with the following schema 我有一个具有以下架构的文件列表

YYYYMMDD.tar.gz

And I want to zcat to each one of them (I dont have zgrep) and then grep each one of them like this 我想zcat到他们每个人(我没有zgrep),然后像这样grep每个人

grep --color=always "STRING" | grep 29000000.00 | grep --color=always string

For the zcat the below is working: 对于zcat,以下代码可以正常工作:

for archivos in $(ls -ltrh 2017091*.tar.gz); do zcat $archivos ; done

If I nested in two for cycles: 如果我嵌套两个周期:

for archivos in $(ls -ltrh 2017091*.tar.gz)
do 
    for subarchivos in $(zcat $archivos)
    do 
        grep --color=always 29000000.00
    done
done

And I get 我得到

gzip: invalid option -- 'w'
Try `gzip --help' for more information.
gzip: 1.gz: No such file or directory
gzip: file.gz: No such file or directory
gzip: file.gz: No such file or directory
gzip: 357M.gz: No such file or directory
gzip: file3.gz: No such file or directory
gzip: 11.gz: No such file or directory
gzip: 00:00.gz: No such file or directory

And just stays there I think is to heavy and is taking a lot of time and I cannot see if it is really working could someone help me? 只是呆在那里,我觉得很沉重,要花很多时间,我看不出它是否真的有效,有人可以帮我吗?

 for archivos in $(ls -ltrh 2017091*.tar.gz); do zcat $archivos ; done 

If you're going to parse ls's output you need to leave out all of those flags. 如果要解析ls的输出,则需要省略所有这些标志。 You only want it to print file names, not sizes and permissions and such. 您只希望它打印文件名,而不打印大小和权限等。 Get rid of -ltrh . 摆脱-ltrh

Except, really, you shouldn't parse the output of ls. 除了,实际上, 您不应该解析ls的输出。 Instead, just pass all the file names to zcat. 相反,只需将所有文件名传递给zcat。

zcat 2017091*.tar.gz

If I nested in two for cycles: 如果我嵌套两个周期:

 for archivos in $(ls -ltrh 2017091*.tar.gz) do for subarchivos in $(zcat $archivos) do grep --color=always 29000000.00 done done 

You wouldn't want to run grep repeatedly. 您不想重复运行grep Instead, you want to run a series of zcat s and pipe the output of all of them to grep. 相反,您要运行一系列zcat ,并将它们的输出通过管道传递到grep。 To do that, you'd pipe the entire for loop to grep : 为此,您需要将整个for循环传递给grep

for archivos in $(ls -ltrh 2017091*.tar.gz)
do
    zcat "$archivos"
done | grep --color=always 29000000.00

And then as discussed above, you don't need a for loop at all. 然后如上所述,您根本不需要for循环。

zcat 2017091*.tar.gz | grep --color=always 29000000.00

For what it's worth, I don't recommend using --color=always . 对于它的价值,我不建议使用--color=always If you pipe your script's output to more commands for additional processing the color codes will mess things up. 如果将脚本的输出传递给更多命令以进行进一步处理,则颜色代码会使事情搞砸。 --color=auto is better. --color=auto更好。

zcat 2017091*.tar.gz | grep --color=auto 29000000.00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM