LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-11-2008, 05:11 AM   #1
ameyapandit
LQ Newbie
 
Registered: Jul 2008
Posts: 2

Rep: Reputation: 0
How to read string in 1 file and search in another file?


dear experts,

I am newbie to shell scripting and need your help writing a script.

Scenario:
I have 2 files with me.
1. source.txt : contains 10 digit number on each line
2. destination.txt : contains 10 digit number following allphanumeric
string including special characters.

What i want to do is:
Read source.txt file line by line.. and search each string (number) in destination.txt file. If the string is found in destination.txt I want to delete that entire line from this file.
and keep doing this untill end of the source file.

The string from source file may occur multiple times in destination file.

Kindly suggest.. I am lost.
 
Old 07-11-2008, 06:02 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you have GNU grep, you can try the -f switch.
 
Old 07-11-2008, 07:08 AM   #3
aldomenico
LQ Newbie
 
Registered: May 2008
Posts: 3

Rep: Reputation: 0
Try this:

while read string
do
# the next line searches and deletes lines that have the string
sed "/${string}/d" destination.txt > dest.tmp

# the next line moves the temporary edited file
# in place for the next iteration
mv dest.tmp destination.txt
done < source.txt

Assuming source.txt contains a list of strings, one per line

Last edited by aldomenico; 07-11-2008 at 07:12 AM. Reason: Hard to read comments
 
Old 07-11-2008, 07:11 AM   #4
ktgajowniczek
LQ Newbie
 
Registered: Oct 2007
Location: Warsaw, Poland
Distribution: Debian
Posts: 7

Rep: Reputation: 0
Quote:
Originally Posted by ghostdog74 View Post
if you have GNU grep, you can try the -f switch.
that is good, but if the alphanumeric part contains numbers (since it can also be "numeric") than you could remove too much from the DESTINATION file

this, maybe not the best, solution will not:
Code:
for SEED in `cat source.txt`; do 
  grep -vE "^$SEED" destination.txt > tmp_destination.txt
  mv tmp_destination.txt destination.txt
done

rm -f tmp_destination.txt

Last edited by ktgajowniczek; 07-11-2008 at 07:26 AM.
 
Old 07-11-2008, 07:15 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
@OP post some samples.
 
Old 07-11-2008, 08:22 AM   #6
aldomenico
LQ Newbie
 
Registered: May 2008
Posts: 3

Rep: Reputation: 0
Good point Karol.

Here's a revised one to be absolutely sure:

while read string
do
sed "/^${string}/d" destination.txt > dest.tmp
mv dest.tmp destination.txt
done < source.txt
 
Old 07-11-2008, 09:14 AM   #7
ameyapandit
LQ Newbie
 
Registered: Jul 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Angry

Quote:
Originally Posted by aldomenico View Post
Try this:

while read string
do
# the next line searches and deletes lines that have the string
sed "/${string}/d" destination.txt > dest.tmp

# the next line moves the temporary edited file
# in place for the next iteration
mv dest.tmp destination.txt
done < source.txt

Assuming source.txt contains a list of strings, one per line
thnx buddy,

I ran this script removing the "mv dest.tmp destination.txt"
of it so that the output file will remain as dest.tmp.
But the dest.tmp file is similar as destination.txt and no strings
has been removed from this file.

I tried below solutions as well.. but nothing works.
sol1. grep -vf source.txt destination.txt >> dest.tmp

sol2. sort -o source.txt source.txt
sort -o destination.txt destination.txt

comm -13 source.txt destination.txt >> dest.tmp
 
Old 07-11-2008, 12:05 PM   #8
rikijpn
Member
 
Registered: Jun 2007
Location: Japan
Distribution: Debian lenny, DSL, Solaris 10
Posts: 157

Rep: Reputation: 33
hey

I think I've got what you need.

Try this
Code:
#!/bin/bash

for FROM_SRCFILE in $(cat source.txt); do
  TEMP1="sed -i '/"$FROM_SRCFILE"/ d' destination.txt"
  eval $TEMP1
done
This will work (it did on my computer?).

You may had have the same problem I had when I ran
Code:
#!/bin/bash
# doesn't work
for FROM_SRCFILE in $(cat source.txt); do
  sed -i '/"$FROM_SRCFILE"/ d' destination.txt
done
in which I case the file didn't change (if anyone knows why I would actually like to know...).

Well, the above script as you see, takes every line of output of the command "cat source.txt" , and puts line by line temporarily in a variable which we just call FROM_SRCFILE. Then puts the command which deletes the line matching this string into another variable called TEMP1 (didn't have much imagination) , and then just uses eval to correctly "evaluate" the variables and runs the command. It does it as many times as lines the source.txt file has.

Last edited by rikijpn; 07-11-2008 at 10:35 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
giving search-string(s) from a file to linux "find" command Fond_of_Opensource Linux - Newbie 3 02-02-2009 06:14 PM
I would like to search a file for a string kevles01@yahoo.co.uk Solaris / OpenSolaris 2 03-28-2008 12:02 PM
Python: search for string in a list or file chess Programming 3 08-22-2007 04:22 PM
Grep String Search, and identify source file. carl0ski Linux - General 4 01-21-2006 08:15 AM
Java read binary file in as string for RegEx mulciber Programming 1 12-18-2005 12:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration