Sponsored Content
Top Forums Shell Programming and Scripting reading two files, comparing, printing when unmatched value is seen Post 302204852 by i.scientist on Thursday 12th of June 2008 05:07:26 PM
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..
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
DIFF(1) 						      General Commands Manual							   DIFF(1)

NAME
diff - print differences between two files SYNOPSIS
diff [-c | -e | -C n] [-br]file1 file2 OPTIONS
-C n Produce output that contains n lines of context -b Ignore white space when comparing -c Produce output that contains three lines of context -e Produce an ed-script to convert file1 into file2 -r Apply diff recursively to files and directories of EXAMPLES
diff file1 file2 # Print differences between 2 files diff -C 0 file1 file2 # Same as above diff -C 3 file1 file2 # Output three lines of context with every diff -c file1 file2 # Same diff /etc /dev # Compares recursively the directories /etc and /dev diff passwd /etc # Compares ./passwd to /etc/passwd DESCRIPTION
the same name, when file1 and file2 are both directories" difference encountered" Diff compares two files and generates a list of lines telling how the two files differ. Lines may not be longer than 128 characters. If the two arguments on the command line are both directories, diff recursively steps through all subdirectories comparing files of the same name. If a file name is found only in one directory, a diagnostic message is written to stdout. A file that is of either block special, character special or FIFO special type, cannot be compared to any other file. On the other hand, if there is one directory and one file given on the command line, diff tries to compare the file with the same name as file in the directory directory. SEE ALSO
cdiff(1), cmp(1), comm(1), patch(1). DIFF(1)
All times are GMT -4. The time now is 05:35 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy