back to basics with GREP and FOR

Hi,

consider this data file - data.dat - with the below contents
this is a test
this is no test
this is also a test

the below script -
for i in `grep a data.dat`
do
echo $i
done

will produce the output --
this
is
a
test
this
is
also
a
test

However, if we use " around the `grep ....`
for i in "`grep a data.dat`"
do
echo $i
done

would give the results as --
this is a test this is also a test

What needs to be done to the for script in order to get the desired output -
this is a test
this is also a test

Is this your exact requirement? If you just want to print the lines then you can very well say followingin your script
#!/bin/ksh
grep a data.dat

If you want to do some processing then you can write the o/p to file and process each line.

hope this helps

I will be doing processing ...

for i in `grep ......`
do
for j in `grep $i .......`
do
......
......
done
done

#!/bin/ksh
FILENAME="$1"
grep a $FILENAME | while read LINE
do
echo "$LINE"
done

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 ****

In your code,
echo "-----" "$LINE" "-----"
Remove the double quotes around $LINE. I think it should work.

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

thanks,
s.

for i in `grep $LINE $FILENAME | cut -d'|' -f2`
Try putting double quotes arnd $LINE