I need to create a script which search in a multi-line file (plain text) for a list of words from another file and after matching the pattern put only the matching word (not the whole line) into a string or redirect to another file.
any idea which commands can do it?
find string script!
find string script!
Azfar Hashmi
Email : azfarhashmi@hotmail.com
Email : azfarhashmi@hotmail.com
it sounds more like you need someone else to create it for you...I need to create a script which...
Watch out for the Manners Taliban!
Isn't it amazing how so many people can type "linuxpakistan.net" into their browsers but not "google.com"?
Isn't it amazing how so many people can type "linuxpakistan.net" into their browsers but not "google.com"?
HI,
I think below this piece of code might do the trick
this is how i can think of it.
I think below this piece of code might do the trick
Code: Select all
#!/bin/bash
#set -x
ORI_FILE=`pwd`/file
STR_FILE=`pwd`/strings
OUT_FILE=`pwd`/match
rm -fr $OUT_FILE
for i in `cat $STR_FILE`
do
cat $ORI_FILE | grep $i &> /dev/null
if [ "$?" = "0" ]; then
echo $i >> $OUT_FILE
fi
done
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
do you remember sets, and set operations? there's a nice operation called 'intersection'...
think about how you can solve the problem with sets.
Code: Select all
A = { 1, 2, 3, a, b, c }
B = { 1, 3, c, f, g }
A intersect B = { 1, 3, c }
Watch out for the Manners Taliban!
Isn't it amazing how so many people can type "linuxpakistan.net" into their browsers but not "google.com"?
Isn't it amazing how so many people can type "linuxpakistan.net" into their browsers but not "google.com"?
thanks Mudasir, your logic works (perhaps it reporting the matched strings not data) just tweaking it to match other requirements.
Azfar Hashmi
Email : azfarhashmi@hotmail.com
Email : azfarhashmi@hotmail.com
This worked.
Now I can use regex as input easily because now its not going to print the input but output.
Code: Select all
cat $ORI_FILE | grep -o -i $i >> $OUT_FILE
Azfar Hashmi
Email : azfarhashmi@hotmail.com
Email : azfarhashmi@hotmail.com
Hmmm..
i forgot about the -o parameter. its been some time since i did some detailed work on linux. perhaps i am getting rusty .
anyways i am happy that some part of my small code worked for you, i did not tried it out.
lambda's approach is really out of the box, his method i think will be more efficient and fast.
i forgot about the -o parameter. its been some time since i did some detailed work on linux. perhaps i am getting rusty .
anyways i am happy that some part of my small code worked for you, i did not tried it out.
lambda's approach is really out of the box, his method i think will be more efficient and fast.
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com