使用sed的输出一段时间读取循环[重复]

问题描述 投票:0回答:3

这个问题在这里已有答案:

我希望while循环忽略空行(空)行和包含#的行

我尝试在输入文件中包含sed -e的/#.*$//'-e'/ ^ $ / d'并将其输入到while循环中。这没用。

file=$(sed -e 's/#.*$//' -e '/^$/d' foo.txt)

while IFS=: read -r f1 f2 f3 f4; do 

Command

done <"$file"
linux bash while-loop
3个回答
4
投票

您试图将该输出作为文件名打开,几乎可以肯定的是。

在子shell中运行sed并将其输出重定向到while循环。

while IFS=: read -r f1 f2 f3 f4; do
    # do something
done < <(sed -e 's/#.*$//' -e '/^$/d' foo.txt)

对于任何对此答案中使用的语法如何工作感兴趣的人,请参阅the bash-hackers' wiki on process substitution,以及为什么这种语法比sed ... | while read ...更好,请参阅BashFAQ #24


0
投票

你可以使用grep

grep -Ev '^\s*$|^\s*#' foo.txt | while IFS=: read -r f1 f2 f3 f4; do 
    Command
done

-2
投票

使用grap-in:

-v, --invert-match        select non-matching lines
© www.soinside.com 2019 - 2024. All rights reserved.