Find numbers from File1 within File2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find numbers from File1 within File2
# 1  
Old 04-04-2009
Find numbers from File1 within File2

Hi all,

Please your help with this.

I have 2 files,

File_1-->contains a column of N numbers
File_2-->contains many lines with other info and numbers from File_1 within it.

I would like to get from File_2 all the lines containing within the same line each of N numbers from File_1 and "DATA=B".

I know how to do it for one number with the command:
Code:
 
awk '/97782397979/&&/DATA=1/' File_2

result for this line(viewing below example of File_1 and File_2)
Code:
97782397979,RECEIPT=3,DATA=1,MONTH=5

but I canīt do it for all numbers within File_1, I couldnīt get a loop for this yet.
Code:
 
File_1
97782397979
97782397980
97782397981
97782397982
97782397983
97782397984
97782397985
.
.
.
File_2
97782397979,RECEIPT=1,DATA=2,MONTH=1
97782397979,RECEIPT=2,DATA=3,MONTH=2
97782397979,RECEIPT=3,DATA=1,MONTH=5
 
97782397980,RECEIPT=1,DATA=2,MONTH=1
97782397980,RECEIPT=2,DATA=3,MONTH=2
97782397980,RECEIPT=3,DATA=1,MONTH=7
97782397981,RECEIPT=1,DATA=2,MONTH=1
97782397981,RECEIPT=2,DATA=3,MONTH=2
97782397981,RECEIPT=3,DATA=1,MONTH=5

Thanks in advance for any help.

Best regards
# 2  
Old 04-04-2009
Try this approch:

Quote:
grep -f File_1 File_2
# 3  
Old 04-04-2009
Hi dennis,

This could be part of solution wanted, Iīve tryed but Iīm not sure why doesnīt work. Anything appears in display after several minutes.

File_1 is 20MB, File_2 is 178MB size.

Maybe someone can modify the line
Code:
 
 
awk '/97782397979/&&/DATA=1/' File_2

but with a loop that takes each line of File_1 like pattern instead of "97782397979" that works for one event only.

Something like

Code:
 
Put in an array File_1
for(i=1,i<=lastline,i++)
 
  awk '/line[i]/&&/DATA=1/' File_2

I confuse how to finish this
Thanks again,
# 4  
Old 04-04-2009
This should work

Quote:
Originally Posted by cgkmal
Hi dennis,

This could be part of solution wanted, Iīve tryed but Iīm not sure why doesnīt work. Anything appears in display after several minutes.

File_1 is 20MB, File_2 is 178MB size.

Maybe someone can modify the line
Code:
 
 
awk '/97782397979/&&/DATA=1/' File_2

but with a loop that takes each line of File_1 like pattern instead of "97782397979" that works for one event only.

Something like

Code:
 
Put in an array File_1
for(i=1,i<=lastline,i++)
 
  awk '/line[i]/&&/DATA=1/' File_2

I confuse how to finish this
Thanks again,

This works

Code:
nawk -F"," 'FILENAME="File_1" {arr[$0]=$0} FILENAME="File_2" {if (arr[$1]&& $3 ~/DATA=1/) {print $0}}' File_1 File_2

# 5  
Old 04-04-2009
Exactlyyy!!!! siquadri, it works perfect.Smilie

Many thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk- Indexing a list of numbers in file2 to print certain rows in file1

Hi Does anyone know of an efficient way to index a column of data in file2 to print the coresponding row in file1 which corresponds to the data in file2 AND 30 rows preceding and after the row in file1. For example suppose you have a list of numbers in file2 (single column) as follows:... (6 Replies)
Discussion started by: Geneanalyst
6 Replies

2. 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

3. Shell Programming and Scripting

awk or any other means to find IP (File1 / MAC (File2)) entries and putting them on File3

Hi everyone, I would like to complete the following could you please find some time and help me to achieve below: File 1 has a list of IP address (more than 1k) File1:1.1.1.1 2.2.2.2 1.1.1.2 3.3.3.3 2.3.3.2File 2 has content like this:Internet 11.165.91.244 0 Incomplete ... (4 Replies)
Discussion started by: redred
4 Replies

4. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

5. Shell Programming and Scripting

Compare and find records of file1 not in file2

hi.. i am using solaris system and ksh and using nawk to get records of file1 not in file2(not line by line comparison). code i am using is nawk 'NR==FNR{a++} !a {print"line:" FNR"->" $0} ' file2 file1 same command with awk runs perfectly on darwin kernel(mac) but in solaris it does line by... (2 Replies)
Discussion started by: Abhiraj Singh
2 Replies

6. Shell Programming and Scripting

Based on column in file1, find match in file2 and print matching lines

file1: file2: I need to find matches for any lines in file1 that appear in file2. Desired output is '>' plus the file1 term, followed by the line after the match in file2 (so the title is a little misleading): This is honestly beyond what I can do without spending the whole night on it, so I'm... (2 Replies)
Discussion started by: pathunkathunk
2 Replies

7. 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

8. 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

9. Shell Programming and Scripting

how to find string from file1 in file2

hi; i am looking for simple search script that find string from file1 in file 2 file 1 contain a loot of string like: 204080111111111 204080222222222 204080333333333 in each row and i would like to take the first row for example 204080111111111 from file1 and find it in file2 when it... (1 Reply)
Discussion started by: kpinto
1 Replies

10. 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
Login or Register to Ask a Question