I want to apply a command to each line of piped stdin like so:
cat file.txt | grep ... | ./filter | wc -l
the problem is ./filter
accepts only a single line of input and gives a single line of output. I've tried xargs
but it spawns a subshell and I can't capture it's output to continue working with the result. Is there an easy way to do that?
CodePudding user response:
If it accepts a single line, then you should put it in a loop if you want to process multiple lines,
cat file.txt |
grep ... |
while read line ; do
echo "$line" | ./filter
done |
wc -l
CodePudding user response:
To call a command for each line you can read a line into a variable and use the variable as a standard input. (Also, let’s avoid UUOC.)
grep ... < file.txt |
while IFS= read -r line; do
./filter <<< "$line"
done |
wc -l
In this case it looks like things may get way easier if you instead write the whole filter
in awk
. Because it will give you wc -l
for free (NR
), plus line and record splitting and filtering better than what grep
can do.