1826325 Members
3474 Online
109692 Solutions
New Discussion

Re: grep script

 
SOLVED
Go to solution
Chern Jian Leaw
Regular Advisor

grep script

HI,

I would like to grep patterns in "file_1.txt" which has the format below, in the file "original.txt" i.e I would like to see if patterns in "file_1.txt" does exist in "original.txt"

#cat file_1.txt
/fs10/my_design
/fs12/schematics
/fs36/optimization
/fs20/shark

#cat original.txt
/fs10/my_design
/fs34/my_Circuits
/fs12/schematics
/fs15/layouts

I wrote a script to check if the patterns in file_1.txt does actually exist in original.txt:

#cat myscript.sh
for i in `cat file_1.txt`
do
cat original.txt|grep $i|tee -a foundPatterns.txt
done

Unfortunately, it does not grep the patterns in "file_1.txt" out from the file "original.txt" although it does exist.

However, when I did a:
#grep "/fs10/my_design" original.txt
/fs10/my_design

it then prints the contents in "original.txt" which matches the pattern specified.

Could someone show me how the script should have been written?

Thanks.
11 REPLIES 11
Thierry Poels_1
Honored Contributor
Solution

Re: grep script

Hi,

a lazy solution :)

cat original.txt file_1.txt | sort | uniq -d

regards,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Leif Halvarsson_2
Honored Contributor

Re: grep script

Hi
This should work (i called the files a and b in my test).

# while read a
> do
> grep $a b
> done /fs10/my_design
/fs12/schematics
Tom Geudens
Honored Contributor

Re: grep script

Hi,
I did this ...
$more myscript.sh
for i in $(cat file_1.txt)
do
cat original.txt | grep $i | tee -a foundPatterns.txt
done
$./myscript.sh
/fs10/my_design
/fs12/schematics
$more foundPatterns.txt
/fs10/my_design
/fs12/schematics

... which is - imho - exactly what it should be ... or am I missing the point here ?

Regards,
Tom
A life ? Cool ! Where can I download one of those from ?
Chern Jian Leaw
Regular Advisor

Re: grep script

Tom,
I did exactly what you did i.e:
#!/bin/sh

for i in $(cat file_1.txt)
do
cat original.txt|grep $i|tee -a foundPatterns.txt
done

but I'm still unable to find the patterns in original.txt which matches those in file_1.txt.

Could you help me out?

Thanks
CJ
T G Manikandan
Honored Contributor

Re: grep script


--------------------------
for i in `cat /tmp/file.txt`
do
grep "$i" /tmp/original.txt
done
--------------------------


Thanks
Tom Geudens
Honored Contributor

Re: grep script

Hi,
Very strange.
I altered the script just a little, but basically it SHOULD work. I attached myscript. Can you execute it ? Can you also execute with "sh -x myscript.sh" and post the output ?

Regards,
Tom
A life ? Cool ! Where can I download one of those from ?
harry d brown jr
Honored Contributor

Re: grep script


SIMPLE:

# grep -f file_1.txt original.txt
/fs10/my_design
/fs12/schematics


# cat file_1.txt
/fs10/my_design
/fs12/schematics
/fs36/optimization
/fs20/shark
# cat original.txt
/fs10/my_design
/fs34/my_Circuits
/fs12/schematics
/fs15/layouts
#


live free or die
harry
Live Free or Die
Rich Wright
Trusted Contributor

Re: grep script

Try "cat -e" on your pattern file to be sure that you do not have any "hidden" characters or trailing spaces.
These could prevent a match.

Rich
Stefan Schulz
Honored Contributor

Re: grep script

Hi,

i think the grep command will do what you want. Try the follwing:

grep -f file_1.txt original.txt

This should give you all entries from file_1.txt which appear in original.txt.

If needed you could add the -x option for eXact matches.

Hope this helps.

Stefan
No Mouse found. System halted. Press Mousebutton to continue.
Stefan Schulz
Honored Contributor

Re: grep script

Had this thread open in my browser for an hour or so until i was able to write an answer.

Harry was faster with exactly the same answer.

Although i was second, i hope our answers will help.

Regards Stefan
No Mouse found. System halted. Press Mousebutton to continue.
Tim D Fulford
Honored Contributor

Re: grep script

Hi

I only read the first few replys. I think the problem with using grep is that there are meta characters in the grep (like - / $ spaces etc).

You might like to alter you script subtly and do

#cat myscript.sh
for i in `cat file_1.txt`
do
grep "$i" original.txt|tee -a foundPatterns.txt
done

Regards

Tim
-