Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Comparison of 2 files in UNIX Post 302145192 by bakunin on Tuesday 13th of November 2007 06:04:47 AM
Old 11-13-2007
You didn't tell us which shell you are using. I don't know if your construction works in bash, but in ksh it won't:

typeset -A comp1=$(cat file1 | cut -d"|" -f2)

will produce an array with exactly one element from your files, as "typeset -A" uses whitespace as delimiter, not newlines. You will see that when you trace the script with "set -xv" and watch the output of "(cat file1 | cut ...)".

Then there is a logical error in your code: the "for str in .... done" cycles through every element of the array comp2[]. You compare each to every element of comp1[]: comp2[1] to comp1[1], then comp2[1] to comp1[2], ..., comp2[1] to comp1[n], comp2[2] to comp1[1], comp2[2] to comp1[2], ... This is probably not what you want to achieve - you want to compare comp1[1] to comp2[1], comp1[2] to comp2[2], etc. This is done by the following (demo-)code:

Code:
(( count = 0 ))
while (( count < ${#comp2[*]} )) ; do
     if [ "${comp2[$count]}" != "${comp1[$count]}" ] ; then
          print - "line $count is different"
     else
          print - "line $count is equal"
     fi
     (( count += 1 ))
done

If you want to *search* in one file for the key you found in the other and don't rely on them being sorted according to the same key then do the following: cycle through one array, searching through the other one. If you find a corresponding value store the number of the array-element found. If this number is not stored you have not found a corresponding element. Again, in a sketchy demo-code:

Code:
(( count1 = 0 ))
(( count2 = 0 ))
(( found = -1 ))
while (( count2 < ${#comp2[*]} )) ; do
     (( count1 = 0 ))
     (( found = -1 ))
     while (( count1 < ${#comp1[*]} )) ; do
          if [ "${comp2[$count2]}" != "${comp1[$count1]}" ] ; then
               (( found = count1 ))
          fi
          (( count1 += 1 ))
     done
     if [[ $found -lt 0 ]] ; then
          print - "no corresponding element to ${comp2[$count2]} found"
     else
          print - "element ${comp2[$count2]} corresponds to ${comp1[$found]}"
     fi
     (( count2 += 1 ))
done


bakunin
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Unix comparison

I am very new to Unix. What are the similiarities and differences between ScoUnix and AIX5 if any? Where might i find the information? Which is better? (1 Reply)
Discussion started by: NewGuy100
1 Replies

2. Shell Programming and Scripting

comparison of 2 files

Kindly help on follows. I have 2 files. One file contains only one column of mobile numbers. And total records in a file 12 million. Second file contains 2 columns mobile numbers and balance. and total records 30 million. I want to find out balance of each data in file 1 corresponding to file 2.... (2 Replies)
Discussion started by: kamal_418
2 Replies

3. Shell Programming and Scripting

Comparison of two files (sh)

Hi, I have a problem with comparison of two files file1 20100101 20090101 20080101 20071001 20121229 file2 19990112 12 456 7 20011131 19 20100101 2 567 1 987 17890709 123 555 and, sh script needs to compare of these two files and give out to me result: 20100101 2 567 1 987 it... (5 Replies)
Discussion started by: shizik
5 Replies

4. Shell Programming and Scripting

comparison of 2 files using unix or awk

Hello, I have 2 files and I want them to be compared in a specific fashion file1: A_1200_1250 A_1251_1300 B_1301_1350 B_1351_1400 B_1401_1450 C_1451_1500 and so on... file2: 1210 1305 1260 1295 1400 1500 1450 1495 Now The script should look for "1200" from A_1200_1250 of... (8 Replies)
Discussion started by: Diya123
8 Replies

5. Shell Programming and Scripting

Comparison of two files

Hi all I have two files which I have to compare that whetehr there is soemthing common or not body, div, table, thead, tbody, tfoot, tr, th, td, p { font-family: "Liberation Sans"; font-size: x-small; } body, div, table, thead, tbody, tfoot,... (2 Replies)
Discussion started by: manigrover
2 Replies

6. Shell Programming and Scripting

File comparison in UNIX columnwise

Hi all, I want to compare two files with same number of rows and columns with records in same order. Just want to highlight the differences in the column values if any. file A 1,kolkata,19,ab 2,delhi,89,cd 3,bangalore,56,ef file2: 1,kolkata,21,ab 2,mumbai,89,gh 3,bangalore,11,kl... (9 Replies)
Discussion started by: prabhat.diwaker
9 Replies

7. Shell Programming and Scripting

Comparison between two files through UNIX script

Hi All , As I am new to unix scripting ,I need a help regarding unix scripting .I have two .txt files .One is source file and another is target file.I need a script through which I can compare those two files.I need a automated comparison report in a directory after comparing between source &... (2 Replies)
Discussion started by: STCET22
2 Replies

8. Shell Programming and Scripting

Need help regarding comparison between two files through UNIX script

Hi All , I am aware of unix command ,but not comforable in putting together in script level.I came to situation where I need to compare between two .txt files fieldwise and need a mismatch report. As I am new to unix script arena ,if anyone can help in the below scenario that will be really... (9 Replies)
Discussion started by: STCET22
9 Replies

9. Shell Programming and Scripting

UNIX file comparison

I have two files which has component name and version number separated by a space cat file1 com.acc.invm:FNS_PROD 94.0.5 com.acc.invm:FNS_TEST_DCCC_Mangment 94.1.6 com.acc.invm:FNS_APIPlat_BDMap 100.0.9 com.acc.invm:SendEmail 29.6.113 com.acc.invm:SendSms 12.23.65 cat file2 ... (8 Replies)
Discussion started by: rakeshtomar82
8 Replies

10. Shell Programming and Scripting

Comparison of files

I have the requirement I have two files cat fileA something anythg nothing everythg cat fileB everythg anythg Now i shld use fileB and compare every line at fileA and get the output as something nothing (3 Replies)
Discussion started by: Priya Amaresh
3 Replies
HTML::Mason::Component(3pm)				User Contributed Perl Documentation			       HTML::Mason::Component(3pm)

NAME
HTML::Mason::Component - Mason Component Class SYNOPSIS
my $comp1 = $m->current_comp; my $comp2 = $m->callers(1); my $comp3 = $m->fetch_comp('foo/bar'); foreach ($comp1,$comp2,$comp3) { print "My name is ".$_->title.". "; } DESCRIPTION
Mason uses the Component class to store components loaded into memory. Components come from three distinct sources: 1. File-based: loaded from a source or object file. 2. Subcomponents: embedded components defined with the "<%def>" or "<%method>" tags. 3. Anonymous: created on-the-fly with the "make_component" Interp method. Some of the methods below return different values (or nothing at all) depending on the component type. The component API is primarily useful for introspection, e.g. "what component called me" or "does the next component take a certain argument". You can build complex Mason sites without ever dealing directly with a component object. CREATING AND ACCESSING COMPONENTS Common ways to get handles on existing component objects include the Request->current_comp, Request->callers, and Request->fetch_comp methods. There is no published "new" method, because creating a component requires an Interpreter. Use the make_component method to create a new component dynamically. Similarly, there is no "execute" or "call" method, because calling a component requires a request. All of the interfaces for calling a component ("<& &>", "$m->comp", "$interp->exec") which normally take a component path will also take a component object. METHODS
attr (name) Looks for the specified attribute in this component and its parents, returning the first value found. Dies with an error if not found. Attributes are declared in the "<%attr>" section. attr_if_exists (name) This method works exactly like the one above but returns undef if the attribute does not exist. attr_exists (name) Returns true if the specified attribute exists in this component or one of its parents, undef otherwise. attributes Returns a hashref containing the attributes defined in this component, with the attribute names as keys. This does not return attributes inherited from parent components. call_method (name, args...) Looks for the specified user-defined method in this component and its parents, calling the first one found. Dies with an error if not found. Methods are declared in the "<%method>" section. create_time A synonym for load_time (deprecated). declared_args Returns a reference to a hash of hashes representing the arguments declared in the "<%args>" section. The keys of the main hash are the variable names including prefix (e.g. $foo, @list). Each secondary hash contains: o 'default': the string specified for default value (e.g. 'fido') or undef if none specified. Note that in general this is not the default value itself but rather a Perl expression that gets evaluated every time the component runs. For example: # does $comp have an argument called $fido? if (exists($comp->declared_args->{'$fido'})) { ... } # does $fido have a default value? if (defined($comp->declared_args->{'$fido'}->{default})) { ... } dir_path Returns the component's notion of a current directory, relative to the component root; this is used to resolve relative component paths. For file-based components this is the full component path minus the filename. For subcomponents this is the same as the component that defines it. Undefined for anonymous components. flag (name) Returns the value for the specified system flag. Flags are declared in the "<%flags>" section and affect the behavior of the component. Unlike attributes, flags values do not get inherited from parent components. is_subcomp Returns true if this is a subcomponent of another component. For historical reasons, this returns true for both methods and subcomponents. is_method Returns true if this is a method. is_file_based Returns true if this component was loaded from a source or object file. load_time Returns the time (in Perl time() format) when this component object was created. method_exists (name) Returns true if the specified user-defined method exists in this component or one of its parents, undef otherwise. methods This method works exactly like the subcomps method, but it returns methods, not subcomponents. This does not return methods inherited from parent components. Methods are declared in "<%method>" sections. name Returns a short name of the component. For file-based components this is the filename without the path. For subcomponents this is the name specified in "<%def>". Undefined for anonymous components. object_file Returns the object filename for this component. parent Returns the parent of this component for inheritance purposes, by default the nearest "autohandler" in or above the component's directory. Can be changed via the "inherit" flag. path Returns the entire path of this component, relative to the component root. scall_method (name, args...) Like item_call_method, but returns the method output as a string instead of printing it. (Think sprintf versus printf.) The method's return value, if any, is discarded. subcomps With no arguments, returns a hashref containing the subcomponents defined in this component, with names as keys and component objects as values. With one argument, returns the subcomponent of that name or undef if no such subcomponent exists. e.g. if (my $subcomp = $comp->subcomps('.link')) { ... } Subcomponents are declared in "<%def>" sections. title Returns a printable string denoting this component. It is intended to uniquely identify a component within a given interpreter although this is not 100% guaranteed. Mason uses this string in error messages, among other places. For file-based components this is the component path. For subcomponents this is "parent_component_path:subcomponent_name". For anonymous components this is a unique label like "[anon 17]". FILE-BASED METHODS The following methods apply only to file-based components (those loaded from source or object files). They return undef for other component types. source_file Returns the source filename for this component. source_dir Returns the directory of the source filename for this component. SEE ALSO
HTML::Mason, HTML::Mason::Devel, HTML::Mason::Request perl v5.14.2 2012-02-04 HTML::Mason::Component(3pm)
All times are GMT -4. The time now is 05:14 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy