Re: comparing two files
On Sun, 30 Dec 2007 12:22:56 -0800, sonal10july wrote:
> I copied the above command in a file and ran the script but it's
> showing all the records from all files. I'm not very much familier with
> awk command .So, following are the steps I performed.
>
> 1. Copied the above command in a file called 'main_script.sh'
>
> ################################################## ####### $cat
> main_script.sh
> #!/bin/ksh
> awk -F'|' 'NR==FNR {v[$1]=$2;}
> v[$1]!=$2 {print FILENAME "|" $0}' Primary.txt Secondary*
>
> ################################################## #######
>
> 2. Ran the script.
>
> ################################################## ####### $ sh
> main_script.sh
>
> Primary.txt|Currency_exchange|25000
> Primary.txt|Sales|21000
> Primary.txt|instruments|120000
> Secondary1.txt|Currency_exchange|25000
> Secondary1.txt|Sales|20000
> Secondary1.txt|instruments|120000
> Secondary2.txt|Currency_exchange|25000
> Secondary2.txt|Sales|20000
> Secondary2.txt|instruments|110000
> Secondary3.txt|Currency_exchange|25000
> Secondary3.txt|Sales|6600
> Secondary3.txt|instruments|9000
> ################################################## #######
>
> Basically It printed the whole contents from all four files. Thanks in
> advance for your .
>
> I'm using korn shell.
> $ echo $SHELL
> /bin/ksh
>
>
> Best Regards
> SS
OK, something is very wrong.
The -F'|' sets the field delimiter to be a vertical bar, which is the
correct value for the data you have shown us.
The "NR==FNR" is an awk idiom, which is true for the first file, and
false for the second and later files. So "NR==FNR { v[$1]=$2}" says "save
in the array 'v' the value of the second field in the element indexed by
the first field".
The second line "v[$1]!=$2" says "If the value stored in the 'v' array
for the first field is not the same as the second field, then do the
action", and the action is "{print FILENAME "|" $0}" which is "print out
the filename, a vertical bar, and the line from the file".
The second line, by definition, must be true for the first file, as the
first line sets the elements of the 'v' array.
When I copy your files I get the following output
Secondary1.txt|Currency_exchange|21000
Secondary2.txt|Currency_exchange|23100
Can you send me your files by email (the email address of this post is
valid)?
You might try changing the program to
#!/bin/ksh
awk -F'|' 'NR==FNR {v[$1]=$2; print "Setting v[" $1 "] to <" $2 ">"}
v[$1]!=$2 {print FILENAME "|" $0 "(" $1 "," $2 ")"}' Primary.txt
Secondary*
as a debugging aid, and letting us see the output (either here or via
email).
Icarus
|