Thanks Sheetal. I was able to proceed to the next level.
Let me re-phrase the script.
file name: data
> cat data
1|one|this is also one|a
2|one|this is also one|b
3|one|this is also one|c
4|one|this is also one|d
modified script
#!/bin/ksh
FILENAME="$1"
grep one $FILENAME | cut -d'|' -f3 | while read LINE
do
echo "-----" "$LINE" "-----" #### The $LINE is "this is also one" .. has embedded space chars
for i in `grep $LINE $FILENAME | cut -d'|' -f2`
do
echo "" $i ""
done
done
Output --
----- this is also one -----
grep: 0652-033 Cannot open is.
grep: 0652-033 Cannot open also.
grep: 0652-033 Cannot open one.
**** one ****
**** one ****
**** one ****
**** one ****
----- this is also one -----
grep: 0652-033 Cannot open is.
grep: 0652-033 Cannot open also.
grep: 0652-033 Cannot open one.
**** one ****
**** one ****
**** one ****
**** one ****
----- this is also one -----
grep: 0652-033 Cannot open is.
grep: 0652-033 Cannot open also.
grep: 0652-033 Cannot open one.
**** one ****
**** one ****
**** one ****
**** one ****
----- this is also one -----
grep: 0652-033 Cannot open is.
grep: 0652-033 Cannot open also.
grep: 0652-033 Cannot open one.
**** one ****
**** one ****
**** one ****
**** one ****
Desired output
----- this is also one -----
**** one ****
**** one ****
**** one ****
**** one ****
----- this is also one -----
**** one ****
**** one ****
**** one ****
**** one ****
----- this is also one -----
**** one ****
**** one ****
**** one ****
**** one ****
----- this is also one -----
**** one ****
**** one ****
**** one ****
**** one ****
The problem is not with the
echo "-----" $LINE "-----"
but, it is in the
for i in `grep $LINE $FILENAME | cut -d'|' -f2`
The reason, the pattern in $LINE has embedded space char in it. So, when grep reads the $LINE, like --
grep this is also one $FILENAME | cut -d'|' -f2
thats where it fails.
The question is how to apply QUOTES around $LINE for grep, so that when grep expands it, it should do it like --
grep "this is also one" $FILENAME | cut -d'|' -f2