Using grep within a while loop

Hi all,

I have the below script to get input but i cannot get grep to work.

input1.txt
AAAAAAAAG

input2.txt
>gi|184009.1| LEAFY-like [Malus x domestica]|AAAAAAAAGSGGGDHLPY

However, when i use grep -f input1.txt input2.txt
i cannot get any output matches (note that the match is underlined).
Is it because of the "|" character in input2.txt? if so, how can i get it to grep the line?

Thanks

Y

Hi.

  1. welcome to the forum.

  2. what system are you using-- AIX, Solaris, Linux ...?

  3. how were the files created?

  4. You mentioned "while" in the title, but you omitted it in the description.

  5. please use CODE and /CODE enclosed in "[ ]" around data and scripts so that viewing is easy.

  6. the grep as you described worked correctly for me.

Best wishes ... cheers, drl

Hi,
the code was

#!/bin/sh

#echo File grep pattern \: $1
#echo  File search file \: $2
#echo File out \: $3

pwd=`pwd`
echo  current working directory $pwd

while read i 
do
  x=`echo $i | sed "s/\n//g"`
  echo $x
  `grep "$x" "$pwd/$2"` >> "$pwd/$3"
  echo =================================
done < "$pwd/$1" 

i created the file in ubuntu (guest in virtualbox running on vista x64)

thanks in advance

Hi.

So where does

grep -f input1.txt input2.txt

fit into this?

cheers, drl

To use CODE tags, select the text with the mouse, then click the button that looks like a hand beneath a rectangle just above the editing box.

`grep "$x" "$pwd/$2"` >> "$pwd/$3"

should be

grep "$x" "$pwd/$2" >> "$pwd/$3"

regards,
Ahamed

I tried to replace the code with
grep -f input1.txt input2.txt but it just do not work on my ubuntu
any idea?

thanks

Y

---------- Post updated at 12:23 PM ---------- Previous update was at 12:22 PM ----------

Hi Ahamed,

it still do not work
is it a ubuntu problem?

thanks
Y

Hi.

Responses like:

are not useful because they do not tell us anything. You need to be specific.

Please post the exact error messages -- copy / paste work well for this ... cheers, drl

Hi all,

thanks so much for the help
I have found out the reason to why the grep do not work
cause i have been saving the file to a shared folder on a virtual ubuntu guest on window 64bit machine
and at the end of line there is an appened "^M" which was not visualised and i have it replaced and things worked!

Thanks again

Y

Hi.

Glad to hear that it worked.

That is one of the reasons that I asked how the files were created ... cheers, drl