Sponsored Content
Top Forums Shell Programming and Scripting Compare two columns in two files and print the difference Post 302445136 by aigles on Saturday 14th of August 2010 04:21:39 AM
Old 08-14-2010
You can do something like that :
Code:
awk '
NR==FNR {
   val1[$5] = $6;
   val2[$5] = "N/A";
   next
}
{
   if (! ($5 in val1)) val1[$5] = "N/A";
   val2[$5] = $6;
}
END {
   for (col in val1) {
      printf "%12s : %9s : %9s\n", col, val1[col], val2[col];
   }
}
' jr1.txt jr2.txt

Inputfile~#1 (jr1.txt)
Code:
. . importing table new 34
. . importing table employee 119
. . importing table jobs 1

Input file #2 (jr2.txt)
Code:
. . importing table employee 120
. . importing table old 763
. . importing table jobs 1

Output:
Code:
        jobs :         1 :         1
         new :        34 :       N/A
         old :       N/A :       763
    employee :       119 :       120

Jean-Pierre.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

to compare two files and to print the difference

suppose one file P1168S P2150L P85L Q597R R1097C Another file P2150L P85L Q597R R1097C R1379C R1587K Then output shud be R1379C R1587K thanks (5 Replies)
Discussion started by: cdfd123
5 Replies

2. Shell Programming and Scripting

compare columns from seven files and print the output

Hi guys, I need some help to come out with a solution . I have seven such files but I am showing only three for convenience. filea a5 20 a8 16 fileb a3 42 a7 14 filec a5 23 a3 07 The output file shoud contain the data in table form showing first field of... (7 Replies)
Discussion started by: smriti_shridhar
7 Replies

3. Shell Programming and Scripting

Compare two files and print the two lines with difference

I have two files like this: #FILE 1 ABCD 4322 26485 JMTJ 5311 97248 XMPJ 4321 58978 #FILE 2 ABCD 4321 26485 JMTJ 5311 97248 XMPJ 4321 68978 What to do: Compare the two files and find those lines that doesn't match. And have a new file like this: #FILE 3 "from file 1" ABCD 4322 26485... (11 Replies)
Discussion started by: kingpeejay
11 Replies

4. Shell Programming and Scripting

Compare selected columns from a file and print difference

I have learned file comparison from my previous post here. Then, it is comparing the whole line. Now, i have a new problem. I have two files with 3 columns separated with a "|". What i want to do is to compare the second and third column of file 1, and the second and third column of file 2. And... (4 Replies)
Discussion started by: kingpeejay
4 Replies

5. Shell Programming and Scripting

compare two columns of different files and print the matching second file..

Hi, I have two tab separated files; file1: S.No ddi fi cu o/l t+ t- 1 0.5 0.6 o 0.1 0.2 2 0.2 0.3 l 0.3 0.4 3 0.5 0.8 l 0.1 0.6 ... (5 Replies)
Discussion started by: vasanth.vadalur
5 Replies

6. Shell Programming and Scripting

Compare selected columns of two files and print whole line with mismatch

hi! i researched about comparing two columns here and got an answer. but after examining my two files, i found out that the first columns of the two files are not unique with each other. all i want to compare is the 2nd and 3rd column. FILE 1: ABS 456 315 EBS 923 163 JYQ3 654 237 FILE 2:... (1 Reply)
Discussion started by: engr.jay
1 Replies

7. Shell Programming and Scripting

Compare columns 2 files and print

File 1 has 16 columns so does File 2 I want to remove all records from File 2 that column 1 and column 16 match between file 1 and file 2 delimter of files is ~ (10 Replies)
Discussion started by: sigh2010
10 Replies

8. Shell Programming and Scripting

awk compare specific columns from 2 files, print new file

Hello. I have two files. FILE1 was extracted from FILE2 and modified thanks to help from this post. Now I need to replace the extracted, modified lines into the original file (FILE2) to produce the FILE3. FILE1 1466 55.27433 14.72050 -2.52E+03 3.00E-01 1.05E+04 2.57E+04 1467 55.27433... (1 Reply)
Discussion started by: jm4smtddd
1 Replies

9. Shell Programming and Scripting

[Solved] awk compare two different columns of two files and print all from both file

Hi, I want to compare two columns from file1 with another two column of file2 and print matched and unmatched column like this File1 1 rs1 abc 3 rs4 xyz 1 rs3 stu File2 1 kkk rs1 AA 10 1 aaa rs2 DD 20 1 ccc ... (2 Replies)
Discussion started by: justinjj
2 Replies

10. Shell Programming and Scripting

Simple awk command to compare two files and print first difference

Hello, I have two text files, each with a single column, file 1: 124152970 123899868 123476854 54258288 123117283 file 2: 124152970 123899868 54258288 123117283 122108330 (5 Replies)
Discussion started by: LMHmedchem
5 Replies
HCREATE(3)						   BSD Library Functions Manual 						HCREATE(3)

NAME
hcreate, hdestroy, hsearch -- manage hash search table LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <search.h> int hcreate(size_t nel); void hdestroy(void); ENTRY * hsearch(ENTRY item, ACTION action); DESCRIPTION
The hcreate(), hdestroy(), and hsearch() functions manage hash search tables. The hcreate() function allocates sufficient space for the table, and the application should ensure it is called before hsearch() is used. The nel argument is an estimate of the maximum number of entries that the table should contain. This number may be adjusted upward by the algorithm in order to obtain certain mathematically favorable circumstances. The hdestroy() function disposes of the search table, and may be followed by another call to hcreate(). After the call to hdestroy(), the data can no longer be considered accessible. The hdestroy() function calls free(3) for each comparison key in the search table but not the data item associated with the key. The hsearch() function is a hash-table search routine. It returns a pointer into a hash table indicating the location at which an entry can be found. The item argument is a structure of type ENTRY (defined in the <search.h> header) containing two pointers: item.key points to the comparison key (a char *), and item.data (a void *) points to any other data to be associated with that key. The comparison function used by hsearch() is strcmp(3). The action argument is a member of an enumeration type ACTION indicating the disposition of the entry if it cannot be found in the table. ENTER indicates that the item should be inserted in the table at an appropriate point. FIND indicates that no entry should be made. Unsuccessful resolution is indicated by the return of a NULL pointer. The comparison key (passed to hsearch() as item.key) must be allocated using malloc(3) if action is ENTER and hdestroy() is called. RETURN VALUES
The hcreate() function returns 0 if the table creation failed and the global variable errno is set to indicate the error; otherwise, a non- zero value is returned. The hdestroy() function does not return a value. The hsearch() function returns a NULL pointer if either the action is FIND and the item could not be found or the action is ENTER and the ta- ble is full. EXAMPLES
The following example reads in strings followed by two numbers and stores them in a hash table, discarding duplicates. It then reads in strings and finds the matching entry in the hash table and prints it out. #include <stdio.h> #include <search.h> #include <string.h> #include <stdlib.h> struct info { /* This is the info stored in the table */ int age, room; /* other than the key. */ }; #define NUM_EMPL 5000 /* # of elements in search table. */ int main(void) { char str[BUFSIZ]; /* Space to read string */ struct info info_space[NUM_EMPL]; /* Space to store employee info. */ struct info *info_ptr = info_space; /* Next space in info_space. */ ENTRY item; ENTRY *found_item; /* Name to look for in table. */ char name_to_find[30]; int i = 0; /* Create table; no error checking is performed. */ (void) hcreate(NUM_EMPL); while (scanf("%s%d%d", str, &info_ptr->age, &info_ptr->room) != EOF && i++ < NUM_EMPL) { /* Put information in structure, and structure in item. */ item.key = strdup(str); item.data = info_ptr; info_ptr++; /* Put item into table. */ (void) hsearch(item, ENTER); } /* Access table. */ item.key = name_to_find; while (scanf("%s", item.key) != EOF) { if ((found_item = hsearch(item, FIND)) != NULL) { /* If item is in the table. */ (void)printf("found %s, age = %d, room = %d ", found_item->key, ((struct info *)found_item->data)->age, ((struct info *)found_item->data)->room); } else (void)printf("no such employee %s ", name_to_find); } hdestroy(); return 0; } ERRORS
The hcreate() and hsearch() functions may fail if: [ENOMEM] Insufficient storage space is available. [EINVAL] A table already exists. SEE ALSO
bsearch(3), lsearch(3), malloc(3), strcmp(3), tsearch(3) STANDARDS
The hcreate(), hdestroy(), and hsearch() functions conform to X/Open Portability Guide Issue 4, Version 2 (``XPG4.2''). HISTORY
The hcreate(), hdestroy(), and hsearch() functions first appeared in AT&T System V UNIX. BUGS
The interface permits the use of only one hash table at a time. BSD
July 6, 2008 BSD
All times are GMT -4. The time now is 09:54 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy