Computing.Net > Forums > Unix > problem with grep in a for loop

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

problem with grep in a for loop

Reply to Message Icon

Name: Anukta c.
Date: October 7, 2002 at 02:36:19 Pacific
OS: HP-UX
CPU/Ram: 256 MB
Comment:

Hi,
I am having a problem with grep while using it in a for loop.
I have a file "ab" that looks like :
(start file)
--comment 1
-- comment 2
hghj
jh

abc bht nef
987 bht pit
--comment 3
890 gyu ppp
456 bht lll
(end file)

I have to pick out all the lines that contain "bht" and perform some operations on them.
I tried using

for i in `cat ab |grep "bht"`
do
echo $i
...do some tasks
done

But this is not giving me the result I want, since instead of having the output for $i in the form
abc bht nef
987 bht pit
456 bht lll
I am getting it in the form
abc
bht
nef
987
bht
pit
456
bht
lll

I need the for loop to be there, since there are additional operations to be done in line-to-line basis for all lines containing "bht".
Can anyone help?
-Anukta



Sponsored Link
Ads by Google

Response Number 1
Name: jimbo
Date: October 7, 2002 at 02:51:06 Pacific
Reply:

How about this?"

for i in `grep " bht " ab` ; do
echo "${i}"
do tasks ........
done

You don't need 'cat' and it looks like the string 'bht' is surrounded by spaces in the ab file.



0

Response Number 2
Name: jimbo
Date: October 7, 2002 at 03:03:59 Pacific
Reply:

This works:

grep " bht " ab | while read line ; do
echo "$line"
do something ......
done

-jim


0

Response Number 3
Name: Anukta C.
Date: October 7, 2002 at 03:05:55 Pacific
Reply:

Thanks,
i tried, but it still doesn't work.

Also, the "echo $i" statement that I put in there is just a debugging msg, to see the way output is formatted. I will not need it in the main program.
My main problem is how to construct the loop.

-Anukta


0

Response Number 4
Name: Anukta C.
Date: October 7, 2002 at 03:07:49 Pacific
Reply:

The 2nd solution worked !!!
Thanks a lot.

-Anukta


0

Response Number 5
Name: jimbo
Date: October 7, 2002 at 05:20:15 Pacific
Reply:

This is how it would work in a for loop:

for i in "`grep " bht " ab`" ; do
echo "$i"
do tasks ........
done

-jim


0

Related Posts

See More



Response Number 6
Name: Sean Miller
Date: October 15, 2002 at 03:50:40 Pacific
Reply:

Your original script would have been okay, except that the variable IFS was not set to carriage-return.

The default is SPACE so consequently...
A B C
D E F
would produce 6 iterations of the loop.

IFS="^M"
for i in `cat file` ....

would count the newline instead, hence 2 iterations as you required.

Best Wishes,

Sean


0

Response Number 7
Name: Pradeep
Date: October 16, 2002 at 16:02:50 Pacific
Reply:

Hello,
I have another similar problem. Please help me out..
I ran this script

cat tmpfil | grep ^r | while read str
do
NodeId=`echo $str | awk '{print $9}'`
echo NODE is $NodeId $DST

if [ $NodeId = $DST ]
then
var=`echo $str | awk '{print $29}'`
CurCount=$var
PktRcv=`expr $PktRcv + $CurCount`
echo Packet Reeived is $PktRcv
fi

done

echo Packet Reeived is $PktRcv

I found that the value of PktRcv becomes 0 after the while...done loop

This does not happen in the for..done loop. But, i can't use the for..done loop because i need the whole line to process inside the loop. Anything i am doing wrong?

Thanks in advance.

Regards,
pradeep


0

Response Number 8
Name: jimbo
Date: October 25, 2002 at 01:34:47 Pacific
Reply:

My guess is that you are using the bourne shell. Run this with ksh instead. Bourne shell runs while loops in a subshell, thus variables are not recognized outside of the loop. ksh does not do this though. second, the use of 'cat' is not necessary:

grep ^r tmpfil | while read str

does it w/ one less pipe.

-jim


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: problem with grep in a for loop

problems with grep www.computing.net/answers/unix/problems-with-grep/6614.html

For Loop Iteration www.computing.net/answers/unix/for-loop-iteration/7267.html

Problems with validation in script! www.computing.net/answers/unix/problems-with-validation-in-script/4307.html