Read each word from File1 and search each file in file2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read each word from File1 and search each file in file2
# 1  
Old 04-22-2009
Data 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  
Old 04-22-2009
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  
Old 04-22-2009
Hi

ignore the text "[/quote]" near done < file1
# 4  
Old 04-22-2009
Thanks

Abinaya
Thanks so much; I will test it;
# 5  
Old 04-22-2009
you can use -f option of grep, if you have it
# 6  
Old 04-23-2009
Quote:
Originally Posted by clem2610
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  
Old 04-23-2009
Code:
for file in *.txt;do
egrep -f file1 $file
done > out.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

awk read in file1, gsub in file2, print to file3

I'm trying to use awk to do the following. I have file1 with many lines, each containing 5 fields describing an individual set. I have file2 which is a template config file with variable space holders to be replaced by the values in file1. I would like to substitute each set of values in file1 with... (6 Replies)
Discussion started by: msmehaffey
6 Replies

3. Shell Programming and Scripting

Search within file1 numbers from list in file2

Hello to all, I hope somebody could help me with this: I have this File1 (real has 5 million of lines): Number Category --------------- -------------------------------------- 8734060355 3 8734060356 ... (6 Replies)
Discussion started by: Ophiuchus
6 Replies

4. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

5. Shell Programming and Scripting

search from file1 and replace into file2

I have 2 files: file1.txt: 1|15|XXXXXX||9630716||0096000||30/04/2012|E|O|X||||20120525135617-30.04.2012|PAT66OLM|STA||||00001|STA_0096000_YYYPPPXTMEX00_20120525135617_02_P.pdf|... (2 Replies)
Discussion started by: pparthiv
2 Replies

6. Shell Programming and Scripting

Adding extra word from file1 to file2

I need to add a word from file1 to file2 accordinggly... file1 contains name of servers and file2 version of server I need that information in a single file so that the format is server_name : version I been trying but havent been able to figure out how to search for a file using sed... ... (14 Replies)
Discussion started by: eponcedeleonc
14 Replies

7. Shell Programming and Scripting

Read Field from file1 and find and replace in file2

Hi All, I have file1 line below: $myName$|xxx Now I need to read the file1 and find for $myName$ in file2 and replace with xxx file1: $myName$|xxx file2: My name is $myName$ expected output in file2 after executing the script is below: my name is xxx Thanks, (8 Replies)
Discussion started by: gdevadas
8 Replies

8. Shell Programming and Scripting

Search & replace fields from file1 to file2

hi, I have two xml files with the name source.xml and tobe_replaced.xml. Sample data: source.xml contains: <?xml version="1.0"?> <product description="prod1" product_info="some/info"> <product description="prod2" product_info="xyz/allinfo"> <product description="abc/partialinfo"... (2 Replies)
Discussion started by: dragon.1431
2 Replies

9. Shell Programming and Scripting

AWK: read values from file1; search for values in file2

I have read another post about this issue and am wondering how to adapt it to my own, much simpler, issue. I have a file of user IDs like so: 333333 321321 546465 ...etc I need to take each number and use it to print records wherein the 5th field matches the user ID pulled from the... (2 Replies)
Discussion started by: Bubnoff
2 Replies

10. Shell Programming and Scripting

Search values between ranges in File1 within File2

Hi people, I have 2 files, one with a list of non consecutive ranges (File1.txt), where each range begins with the value in column 1 and finishes with the value in column 2 in the same line, as can be seen above. 215312581156279 215312581166279 215312582342558 215312582357758... (4 Replies)
Discussion started by: cgkmal
4 Replies
Login or Register to Ask a Question