Thursday, October 27, 2022

[SOLVED] Use output of sed for a while read loop

Issue

I want the while loop to ignore blank (empty) lines and lines that contain #

I tried including sed -e 's/#.*$//' -e '/^$/d' on the input file and piping this into the while loop. This did not work.

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

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

Command

done <"$file"

Solution

You're trying to open that output as a filename, which it almost certainly isn't.

Run sed in a subshell and redirect its output to while loop instead.

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

For anyone interested in how the syntax used in this answer works, see the bash-hackers' wiki on process substitution, and for why this syntax is better than sed ... | while read ..., see BashFAQ #24.



Answered By - oguz ismail
Answer Checked By - Marilyn (WPSolving Volunteer)