The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search, replace string in file1 with string from (lookup table) file2? gstuart Shell Programming and Scripting 9 06-08-2009 07:11 AM
grep -f file1 file2 vijay_0209 Shell Programming and Scripting 7 03-05-2009 05:48 AM
Search values between ranges in File1 within File2 cgkmal Shell Programming and Scripting 4 01-31-2009 06:37 PM
awk/sed search lines in file1 matching columns in file2 floripoint Shell Programming and Scripting 1 12-17-2008 11:36 PM
Awk Compare File1 File2 on f2 RacerX Shell Programming and Scripting 4 10-27-2008 09:50 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-22-2009
clem2610 clem2610 is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 5
Unhappy Read each word from File1 and search each file in file2

file1: has all words to be searched.
100007
200999
299997

File2: has all file names to be searched.

C:\search1.txt
C:\search2.txt
C:\search3.txt
C:\search4.txt

Outfile: should have all found lines.

Logic: Read each word in file1 and search each file in the list of File2; if the word found; print the entire line in outfile.

Pls help, thanks in advance.
  #2 (permalink)  
Old 04-22-2009
abinaya abinaya is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 24
Hi,

Check the below code

more search.sh
#!/bin/sh

while read line
do
stxt=`echo $line`
for i in `cat file2`
do
ftxt=`grep -i $stxt $i`
if [ -n "$ftxt" ];then
echo $ftxt >> Outfile
fi
done
done < file1[/QUOTE]
  #3 (permalink)  
Old 04-22-2009
abinaya abinaya is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 24
Hi

ignore the text "[/quote]" near done < file1
  #4 (permalink)  
Old 04-22-2009
clem2610 clem2610 is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 5
Thanks

Abinaya
Thanks so much; I will test it;
  #5 (permalink)  
Old 04-22-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,555
you can use -f option of grep, if you have it
  #6 (permalink)  
Old 04-23-2009
devtakh devtakh is offline
Registered User
  
 

Join Date: Oct 2007
Location: Bangalore
Posts: 514
Quote:
Originally Posted by clem2610 View Post
file1: has all words to be searched.
100007
200999
299997

File2: has all file names to be searched.

C:\search1.txt
C:\search2.txt
C:\search3.txt
C:\search4.txt

Outfile: should have all found lines.

Logic: Read each word in file1 and search each file in the list of File2; if the word found; print the entire line in outfile.

Pls help, thanks in advance.
while read string
do
while read filename
do
if [[ `grep -c "$string" $filename` > 0 ]] then
grep "$string" $filename
fi
done < file2
done < file1


cheers,
Devaraj Takhellambam
  #7 (permalink)  
Old 04-23-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,089

Code:
for file in *.txt;do
egrep -f file1 $file
done > out.txt

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:09 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0