Operating System - HP-UX
1827427 Members
4101 Online
109965 Solutions
New Discussion

Re: Problem in out of greping.

 
SOLVED
Go to solution
N Gopal
Frequent Advisor

Problem in out of greping.

Hi,

I am having one .dat file(MEMBER_ALL.dat) which having entries like:

"DMNH01-03-200710:10:59666682229 MID MMEDSCZFHI1234567999hgjnrfoodoJrGF kxzgF Y I 7546713742B Y 16-04-1940 01-12-200615-12-2006RAMYY U14-11-2005Y BY000050000000040000 123456789 1C NABIPW987654321987001 DM24100005"

this is complete 1 line. Similarly he is having sevral lines.

What i am doing is like..

while read line

do

refkey=`echo "$line" | cut -c1-2`
if [ "$refkey" = "DM" ]; then
echo $line | grep $refkey >> DM.dat
fi;

done
Basically it will cut first 2 char and then redirect the complete line in other .dat file.

But when i see the output it is suppressing the spaces between two vaules. I want exact line what is present in input file(MEMBER_ALL).

Can anyone suggest how to do this?

Thanks

7 REPLIES 7
Dennis Handly
Acclaimed Contributor

Re: Problem in out of greping.

You shouldn't be using read. Where you might be able to fiddle with IFS to prevent it, it is simple to just use grep:
$ grep ^DM MEMBER_ALL > DM.dat

With your "if", this line will always match:
echo $line | grep $refkey
And is the same as:
echo $line
Laurent Menase
Honored Contributor
Solution

Re: Problem in out of greping.

Your script looks a little strange.
Why don't you do just a

grep "^DM" MEMBER_ALL >DM.dat

Laurent Menase
Honored Contributor

Re: Problem in out of greping.

else if you really want to use your script like that:
just replace echo $line by echo "$line".


Dennis Handly
Acclaimed Contributor

Re: Problem in out of greping.

>Laurent: just replace echo $line by echo "$line".

You're right. It wasn't the read but the echo.
Sandman!
Honored Contributor

Re: Problem in out of greping.

The if test will be false because the cut(1) command pickups only the double-quote and the letter D. Besides as pointed out by Dennis and Laurent grep(1) is the way to go i.e.

# grep ^\"DM MEMBER_ALL.dat
Dennis Handly
Acclaimed Contributor

Re: Problem in out of greping.

>Sandman: pickups only the double-quote and the letter D

Since Gopal is getting some output, those "" must not really be there in the file.
N Gopal
Frequent Advisor

Re: Problem in out of greping.

Hi All,

Very much for your replies. Thanks to Dennis and Laurent's.
This statement is working for me.
grep "^DM" MEMBER_ALL >DM.dat

Thanks