linux 将sed的输出用于while读取循环[duplicate]

a0x5cqrl  于 2022-11-02  发布在  Linux
关注(0)|答案(3)|浏览(107)

此问题在此处已有答案

Loop through results of sed(2个答案)
Reading lines in a file and avoiding lines with # with Bash(共10个答案)
三年前就关门了。
我希望while循环忽略空白行和包含#的行
我试着在输入文件中包含sed -e 's/#.*$//' -e '/^$/d',并将其管道化到while循环中,但没有成功。

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

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

Command

done <"$file"
hjqgdpho

hjqgdpho1#

  • 您试图以文件名的形式打开该输出,但几乎可以肯定不是这样。*

在subshell中运行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

ddarikpa

ddarikpa2#

您可以使用grep

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

xdnvmnnf3#

使用grep -v:

-v, --invert-match        select non-matching lines

相关问题