I’m trying to extract fields from a pipe-delimited file and provide them as arguments to an external program in a loop. The file contains lines like this:
value1|value2
value3|value4
So I came up with:
while read line;
do echo -n "${line}" | awk -F '|' '{print $1}';
echo -n " something ";
echo -n "${line}" | awk -F '|' '{print $2}';
echo " somethingelse";
done < <(cat $FILE)
I want to see the following output:
value1 something value2 somethingelse
value3 something value4 somethingelse
But instead I’m getting:
value1
something value2
somethingelse
value3
something value4
somethingelse
Perhaps I shouldn’t be using echo?
If your
awksupportsprintf, I would use that instead. Even thoughecho -ndoesn’t add a newline,awk {print}does. Theprintfvariant will not put the newline at the end likeprintdoes:Here’s your version hex-dumped, note the
\nat the end when you pass it throughawk:and here’s the
printfversion:And here it is, running on your real file (named FILE in this example to ease my testing):