Redirect output of command line to for loop

I would like to redirect output of command line in for loop as $line.

Output should be processed as line but instead it throw whole output.

Could somebody help me on how to redirect output of command line and process it line by line without sending output to any file.

below is my code

result=`${HOME}/sqlplus << EOF
command
select * from xyz;
exit
EOF`
###############################
for line in ${result}
do
echo $line
echo ${result} | grep xxx

Now

${HOME}/sqlplus << EOF
command
select * from xyz;
exit

produce output like below .

xxx   yyyy 11111   22222
xxx   yyyy 33333   44444
aaa  bbbb  5555    66666
aaa  bbbb  7777    99999
$ sqlplus -S <<! | while read; do printf 'line: %s\n' "$REPLY"; done
> / as sysdba
> select sysdate from dual;
> !
line:
line: SYSDATE
line: ---------
line: 08-JAN-13
line:

Or:

$ sqlplus -S <<! |
> / as sysdba
> select sysdate from dual;
> !
> while read; do
>   printf 'line: %s\n' "$REPLY"
> done
line:
line: SYSDATE
line: ---------
line: 08-JAN-13
line:

In your case it would be something like this:

"$HOME"/sqlplus << EOF | 
command
select * from xyz;
exit
EOF 
while IFS= read -r; do
  printf '%s\n' "$REPLY" | grep whatever
done

P.S. Drop the -r option of read if your shell doesn't support it.

1 Like