find string script!

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.

find string script!

Postby azfar » Fri May 14, 2010 4:34 pm

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
azfar
Captain
 
Posts: 598
Joined: Tue Mar 23, 2004 1:16 am
WLM: azfarhashmi@hotmail.com
Yahoo Messenger: azfarhusain@yahoo.com
Location: Karachi

Postby lambda » Fri May 14, 2010 7:00 pm

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"?
lambda
Major General
 
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Website: http://www.hungry.com/~fn/
Location: Lahore

Postby mudasir » Fri May 14, 2010 8:58 pm

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
mudasir
Captain
 
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Website: http://www.crystalnetworks.org
Location: Dubai

Postby lambda » Sat May 15, 2010 8:01 am

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"?
lambda
Major General
 
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Website: http://www.hungry.com/~fn/
Location: Lahore

Postby azfar » Sat May 15, 2010 1:34 pm

thanks everyone let me see them.
Azfar Hashmi

Email : azfarhashmi@hotmail.com
azfar
Captain
 
Posts: 598
Joined: Tue Mar 23, 2004 1:16 am
WLM: azfarhashmi@hotmail.com
Yahoo Messenger: azfarhusain@yahoo.com
Location: Karachi

Postby azfar » Sat May 15, 2010 3:11 pm

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
WLM: azfarhashmi@hotmail.com
Yahoo Messenger: azfarhusain@yahoo.com
Location: Karachi

Postby azfar » Sat May 15, 2010 3:58 pm

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
azfar
Captain
 
Posts: 598
Joined: Tue Mar 23, 2004 1:16 am
WLM: azfarhashmi@hotmail.com
Yahoo Messenger: azfarhusain@yahoo.com
Location: Karachi

Postby mudasir » Tue May 18, 2010 12:16 am

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
mudasir
Captain
 
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Website: http://www.crystalnetworks.org
Location: Dubai


Return to “%s” Programming

Who is online

Users browsing this forum: No registered users and 2 guests

cron