reading two files, comparing, printing when unmatched value is seen


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting reading two files, comparing, printing when unmatched value is seen
# 1  
Old 06-12-2008
reading two files, comparing, printing when unmatched value is seen

Hello,
I have two files:
file1
1
2
3
4
5
file2
"a","b",,,,,"c","1",.....
"s","d",,,,,"s","1",.....
"a","c",,,,,"d","1",....
"f","v",,,,,,"f","2",.....
etc

I have to read "1" from file1 and grab all records in file2 (say column 6 in file2 is "1") until the column changes its value ( here it changes to 2 ).
go back to file 1, get next value (2) and then grab all values in file 2 till it gets a different value (may not be 3, different than 2) etc..and continue.

I tried this with While ...do ..done...it takes lot of time due to the size of file 2.
(file2 will have 30 columns). I hope you understand my question.

this is a rough script on which i was working on. i do not want to use two while loops
while read number
do
while read column
do
var = `echo read column | cut -d -6f "," |
if $number == $var
then
do ....
..

done < file2

done < file1

Last edited by i.scientist; 06-12-2008 at 08:48 AM.. Reason: script addition
# 2  
Old 06-12-2008
Hammer & Screwdriver Not actual coding, but perhaps a direction to take?

Code:
#read first file, pull out 6th field
#place at beginning with pipe delimiter for easier retrieval
while read zf
  do
  var = $(echo "$zf" | cut -d"," -f6)
  echo "$var""|""$zf" >> file2a
done <file2

#read thru first file, creating a temp file for ultimate processing
while read yf
  do
#select the appropriate variable data
  chk=$(echo "$yf" | cut -f1)
  cat $file2 | cut "^$chk|" >file2b
done <file1

#do the processing
while read xf
   do
   ** whatever
done <$file2b

This should decrease the number of times you need to cycle-thru that large second file. Or, is a different thought-logic for trying to solve the problem at hand. (Warning - I have not tested the coding; just off the top of my head thinking thru your issue.)
# 3  
Old 06-12-2008
I think I was not clear about my question. Let us say we have file1 and file2.

file1
1234
1235
1236
1237
1238
etc

file2

"1","1234","4","3",,,,,," 123.34","qw","as",...etc
"1","1235","4","3",,,,,," 213.34","qw","as",...etc
"1","1235","4","3",,,,,," 765.84","qw","as",...etc
"1","1236","4","3",,,,,," -123.74","qw","as",...etc
"1","1236","4","3",,,,,," 123.90","qw","as",...etc
"1","1236","4","3",,,,,," -213.34","qw","as",...etc
"1","1237","4","3",,,,,," 123.34","qw","as",...etc
"1","1237","4","3",,,,,," 123.34","qw","as",...etc
"1","1238","4","3",,,,,," 123.34","qw","as",...etc
etc

now goto file1, get first number....corr number in file 2 is column2.
grab all those records in file2 which corr to this number in column 2 of file2.
Also calculate the net amount for those records of column in bold.

Assuming file1 and file2 are sorted . (file2 based on 2nd column), start with first number in file 1 and grab all records corr to column 2 in file2. If not matched, leave file2. Now goto file1 , next number...comeback to file2 at the same point where we left earlier.(since they are sorted) and grab all records corr. to second column....continue.
Quote:
while read file1
do

while read file2
do
var0=`echo $file2 | cut -f 2 -d "," |sed -e "s/\"//g"`
var1=`echo $file2 | cut -f 7 -d "," |sed -e "s/\"//g"`
var2=`echo $file2 | sed -e "s/ //g"`
if $file1 == $var0
typeset -f var2=$var3
sum=`echo $sum + $var3| bc`
done<file2
done<file1
I am trying something like above...pls suggest

Last edited by i.scientist; 06-12-2008 at 10:24 PM..
# 4  
Old 06-12-2008
Code:
awk 'BEGIN{FS=","}
FNR==NR{
 gsub("\"","",$2)
 gsub(/\"| +/,"",$10)
 total[$2]+=$10
 next
} 
{
  print $0,total[$0]
}
' file2 file1

# 5  
Old 06-13-2008
it works!!!

thank you. i am not good at awk. but i can understand what you did.

can we write the output to a file ?
for example...in the same example it prints the totals. so can we say print the entire row from file2 into a new file say file3 if total is positive ?

I am not sure how we can redirect the output ?
And how to obtain the entire row (file2) using awk ?


thanks.
# 6  
Old 06-13-2008
Quote:
Originally Posted by i.scientist
can we write the output to a file ?
use redirection ">"
Code:
....
 {
   if ( total[$0] > 0 ){
    print $0,total[$0]  > "file3"
   }
}
....

Quote:
And how to obtain the entire row (file2) using awk ?
just use $0
# 7  
Old 06-13-2008
Quote:
Originally Posted by ghostdog74
use redirection ">"
Code:
....
 {
   if ( total[$0] > 0 ){
    print $0,total[$0]  > "file3"
   }
}
....


just use $0
How to get file2 rows ?
Can I just say print $0 in above loop ?
when i do above print $0 first file rows are printing and not second file rows.
Do i need to give the print command outside the loop ?

What I mean is If total is more than zero... print all rows of file2 corr. to this number.

file2
"1","1","4","3",,,,,," 123.34"
"1","1","4","3",,,,,," 213.34"
"1","2","4","3",,,,,," -765.84"
"1","2","4","3",,,,,," -123.74"
"1","3","4","3",,,,,," 123.90"
"1","3","4","3",,,,,," -213.34"
"1","4","4","3",,,,,," -123.34"
"1","5","4","3",,,,,," 123.34"
"1","6","4","3",,,,,," -123.34"

file1
1
2
3
4
5
6

output should be like
"1","1","4","3",,,,,," 123.34"
"1","1","4","3",,,,,," 213.34"
"1","5","4","3",,,,,," 123.34"

Last edited by i.scientist; 06-13-2008 at 01:36 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing two columns in two files and printing a third based on a match

Hello all, First post here. I did not notice a previous post to help me down the right path. I am looking to compare a column in a CSV file against another file (which is not a column match one for one) but more or less when a match is made, I would like to append a third column that contains a... (17 Replies)
Discussion started by: dis0wned
17 Replies

2. Shell Programming and Scripting

Comparing same column from two files, printing whole row with matching values

First I'd like to apologize if I opened a thread which is already open somewhere. I did a bit of searching but could quite find what I was looking for, so I will try to explaing what I need. I'm writing a script on our server, got to a point where I have two files with results. Example: File1... (6 Replies)
Discussion started by: mitabrev83
6 Replies

3. Shell Programming and Scripting

Reading and Comparing values of file

Hi gurus.. Am reading a file, counting number of lines and storing it in a variable. Then am passing that variable into If loop for comparision, if the number of lines are greater than 1000 it should split a file if not it should send the file name to archive folder.. but when i execute the... (4 Replies)
Discussion started by: azherkn3
4 Replies

4. UNIX for Dummies Questions & Answers

Comparing two test files and printing out the values that do not match

Hi, I have two text files with matching first columns. Some of the values in the second column do not match. I want to write a script to print out the rows (only the first column) where the values in the second column do not match. Example: Input 1 A 1 B 2 C 3 D 4 Input 2 A 2 B 2... (6 Replies)
Discussion started by: evelibertine
6 Replies

5. UNIX for Dummies Questions & Answers

Comparing two text files by a column and printing values that do not match

I have two text files where the first three columns are exactly the same. I want to compare the fourth column of the text files and if the values are different, print that row into a new output file. How do I go about doing that? File 1: 100 rs3794811 0.01 0.3434 100 rs8066551 0.01... (8 Replies)
Discussion started by: evelibertine
8 Replies

6. UNIX for Dummies Questions & Answers

Comparing the 2nd column in two different files and printing corresponding 9th columns in new file

Dear Gurus, I am very new to UNIX. I appreciate your help to manage my files. I have 16 files with equal number of columns in it. Each file has 9 columns separated by space. I need to compare the values in the second column of first file and obtain the corresponding value in the 9th column... (12 Replies)
Discussion started by: Unilearn
12 Replies

7. Shell Programming and Scripting

print unmatched data from 2 files

Hi, i need help to extract unmatched data from 2 files. for instance, the input file like below: file1.txt ID hit addhit 123 sp q9876 656 sp q5641 332 sp d1211 248 sp f5642file2.txt ID Num def activity 123 447 trial ... (4 Replies)
Discussion started by: redse171
4 Replies

8. Programming

help with C programming (reading from files and printing them) (not C++)

I have a file called dvwl.c, and I am running it on a putty (unix server) using: gcc -Wall -g -o mycode dvwl.c ./mycode 1 /usr/share/dict/words s What it does is, it opens up words (since i gave that path) and reads the lines skipping the first line (since it says 1, if i put here 3, then it... (1 Reply)
Discussion started by: omega666
1 Replies

9. Shell Programming and Scripting

Comparing two files and printing 2nd column if match found

Hi guys, I'm rather new at using UNIX based systems, and when it comes to scripting etc I'm even newer. I have two files which i need to compare. file1: (some random ID's) 451245 451288 136588 784522 file2: (random ID's + e-mail assigned to ID) 123888 xc@xc.com 451245 ... (21 Replies)
Discussion started by: spirm8
21 Replies

10. Shell Programming and Scripting

Comparing Columns and printing the difference from a particular file

Gurus, I have one file which is having multiple columns and also this file is not always contain the exact columns; sometimes it contains 5 columns or 12 columns. Now, I need to find the difference from that particular file. Here is the sample file: param1 | 10 | 20 | 30 | param2 | 10 |... (6 Replies)
Discussion started by: buzzusa
6 Replies
Login or Register to Ask a Question