gpt4 book ai didi

linux - 在 Shell 中遍历 grep 返回的行

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:55:27 24 4
gpt4 key购买 nike

假设我有一个文件 info.txt。第一列是id,其余是它的内容。

 1 aaa bbb
1 ccc ddd mmm
4 ccc eee
7 ddd fff jjj kkk

我只对以“1”开头的行感兴趣。所以我使用 grep 来过滤它:

what_I_concern=$(cat info.txt | grep -iw 1 | cut -d ' ' -f 2-)

然后我想遍历这些行:

 for i in $what_I_concern; do
pass $i to another program #I want to pass one line at a time
done

但它真正做的是遍历这些行中的每个单词,而不是将每一行作为一个整体。

我该如何解决这个问题?

最佳答案

您尝试完成所需任务的方式导致了分词。相反,说:

while read -r line; do
someprogram $(cut -d' ' -f2- <<< "$line")
done < <(grep '^1' info.txt)

<()语法称为 Process Substitution .在这种情况下,它启用 while循环读取 grep 的输出命令作为文件。

关于linux - 在 Shell 中遍历 grep 返回的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20138397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com