find string script!

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.
Post Reply
azfar
Captain
Posts: 598
Joined: Tue Mar 23, 2004 1:16 am
Location: Karachi
Contact:

find string script!

Post by azfar »

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?
Azfar Hashmi
Email : azfarhashmi@hotmail.com
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Post by lambda »

I need to create a script which...
it sounds more like you need someone else to create it for you...
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"?
mudasir
Captain
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Location: Dubai
Contact:

Post by mudasir »

HI,

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
this is how i can think of it.
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Post by lambda »

do you remember sets, and set operations? there's a nice operation called 'intersection'...

Code: Select all

A  = { 1, 2, 3, a, b, c }
B = { 1, 3, c, f, g }

A intersect B = { 1, 3, c }
think about how you can solve the problem with sets.
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"?
azfar
Captain
Posts: 598
Joined: Tue Mar 23, 2004 1:16 am
Location: Karachi
Contact:

Post by azfar »

thanks everyone let me see them.
Azfar Hashmi
Email : azfarhashmi@hotmail.com
azfar
Captain
Posts: 598
Joined: Tue Mar 23, 2004 1:16 am
Location: Karachi
Contact:

Post by azfar »

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
azfar
Captain
Posts: 598
Joined: Tue Mar 23, 2004 1:16 am
Location: Karachi
Contact:

Post by azfar »

This worked.

Code: Select all

cat $ORI_FILE | grep -o -i $i >> $OUT_FILE
Now I can use regex as input easily because now its not going to print the input but output.
Azfar Hashmi
Email : azfarhashmi@hotmail.com
mudasir
Captain
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Location: Dubai
Contact:

Post by mudasir »

Hmmm..
i forgot about the -o parameter. its been some time since i did some detailed work on linux. perhaps i am getting rusty :D.

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
Post Reply