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
Not actual coding, but perhaps a direction to take?
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.)
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.
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 ?
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)