Sponsored Content
Top Forums Shell Programming and Scripting Compare two files and write data to second file using awk Post 302898661 by Ajay Venkatesan on Wednesday 23rd of April 2014 08:32:46 AM
Old 04-23-2014
Tools Compare two files and write data to second file using awk

Hi Guys,

I wanted to compare a delimited file and positional file, for a particular key files and if it matches then append the positional file with some data.

Example:

Delimited File
--------------
Code:
Byer;Amy;NONE1;A5218257;E5218257
Byer;Amy;NONE1;A5218260;E5218260

Positional File
--------------
Code:
190004000153E5218257010000000980002425852013
190004000153E5218258010000003850002425852013
190004000153E5218259010000012190002425852013
190004000153E5218260010000003850002425852013
190004000153E5218261010000012190002425852013

If the fifth filed in the delimited file is found in the positional file at position 13 then we have to append the fourth field from the delimited file into the positional file, at the end of the respective record.

Expected output
===============
Code:
190004000153E5218257010000000980002425852013A5218257
190004000153E5218258010000003850002425852013
190004000153E5218259010000012190002425852013
190004000153E5218260010000003850002425852013A5218260
190004000153E5218261010000012190002425852013

Script Used
===========
Code:
awk 'BEGIN{FS = ";"};FNR==NR{A[$5] = $4}
FNR!=NR{if ((x = substr($0,13,8)) in A) print $0 A[x];else print A[x]}' $1 $2 > $3


The above awk command is not working properly. It is not appending the desired data in the positional file.

Please help.

Thanks in advance!!

Thanks,
Ajay

Last edited by Scrutinizer; 04-23-2014 at 09:44 AM.. Reason: Please use code tags; mod2: spelling
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare data in 2 files and delete if file exist

Hi there, I have written a script called "compare" (see below) to make comparison between 2 files namely test_put.log and Output_A0.log #!/bin/ksh while read file do found="no" while read line do echo $line | grep $file > /dev/null if then echo $file found found="yes" break fi... (3 Replies)
Discussion started by: lweegp
3 Replies

2. Shell Programming and Scripting

Need to compare two csv files values and write into another csv file

Hi all, Am new to scripting. So i just need your ideas to help me out. Here goes my requirement. I have two csv files 1.csv 2.csv abc,1.24 abc,1 def,2.13 def,1 I need to compare the first column of 1.csv with 2.csv and if matches then need to compare... (2 Replies)
Discussion started by: chinnahyd
2 Replies

3. Shell Programming and Scripting

Compare two csv files by two colums and create third file combining data from them.

I've got two large csv text table files with different number of columns each. I have to compare them based on first two columns and create resulting file that would in case of matched first two columns include all values from first one and all values (except first two colums) from second one. I... (5 Replies)
Discussion started by: agb2008
5 Replies

4. Shell Programming and Scripting

Extract data with awk and write to several files

Hi! I have one file with data that looks like this: 1 data data data data 2 data data data data 3 data data data data . . . 1 data data data data 2 data data data data 3 data data data data . . . I would like to have awk to write each block to a separate file, like this: 1... (3 Replies)
Discussion started by: LinWin
3 Replies

5. Shell Programming and Scripting

How to compare data from 2 zip files and capture the new records from file2 to a new file

I have 2 zip files which have about 20 million records in each file. file 2 will have additional records than file 1. I want to compare the records in both the files and capture the new records from file 2 into another file file3. Please help me with a command/script which provides me the desired... (8 Replies)
Discussion started by: koneru
8 Replies

6. Shell Programming and Scripting

compare 2 files and extract the data which is not present in other file with condition

I have 2 files whose data's are as follows : fileA 00 lieferungen 00 attractiop 01 done 02 forness 03 rasp 04 alwaysisng 04 funny 05 done1 fileB alwayssng dkhf fdgdfg dfgdg sdjkgkdfjg funny rasp (7 Replies)
Discussion started by: rajniman
7 Replies

7. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 Replies

8. Shell Programming and Scripting

Request to check: compare two files , match same entries, write data before it

Hi all, I have 2 files:Column1 of first file has to be matched with column 3 of second file first file contain DATA like this in 2 columns one with gene name second with whether CAD,HT,RA T2Dor any one column 1 column2 ARFGEF2 CAD DDEF2 CAD PSCD3 CAD PSCD4 CAD CAMK1... (5 Replies)
Discussion started by: manigrover
5 Replies

9. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

10. Shell Programming and Scripting

Compare 2 files and extract the data which is present in other file - awk is not working

file2 content f1file2 content f1,1,2,3,4,5 f1,2,4,6,8,10 f10,1,2,3,4,5 f10,2,4,6,8,10 f5,1,2,3,4,5 f5,2,4,6,8,10awk 'FNR==NR{a;next}; !($1 in a)' file2 file1output f10,1,2,3,4,5 f10,2,4,6,8,10 f5,1,2,3,4,5 f5,2,4,6,8,10awk 'FNR==NR{a;next}; ($1 in a)' file2 file1output nothing... (4 Replies)
Discussion started by: gksenthilkumar
4 Replies
Kwargs(3pm)						User Contributed Perl Documentation					       Kwargs(3pm)

NAME
Kwargs - Simple, clean handing of named/keyword arguments. VERSION
version 0.01 SYNOPSIS
use Kwargs; # just named my ($foo, $bar, baz) = kw @_, qw(foo bar baz); # positional followed by named my ($pos, $opt_one, $opt_two) = kwn @_, 1, qw(opt_one opt_two) # just a hashref my $opts = kw @_; # positional then hashref my ($one, $two, $opts) = kwn @_, 2; WHY
? Named arguments are good, especially when you take lots of (sometimes optional) arguments. There are two styles of passing named arguments (by convention) in perl though, with and without braces: sub foo { my $args = shift; my $bar = $args->{bar}; } foo({ bar => 'baz' }); sub bar { my %args = @_; my $foo = $args{foo}; } bar(foo => 'baz'); If you want to support both calling styles (because it should be mainly a style issue), then you have to do something like this: sub foo { my $args = ref $_[0] eq 'HASH' ? $_[0] : { @_ }; my $bar = $args->{bar}; } Which is annoying, and not even entirely correct. What if someone wanted to pass in a tied object for their optional arguments? That could work, but what are the right semantics for checking for it? It also gets uglier if you want to unpack your keyword arguments in one line for clarity: sub foo { my ($one, $two, $three) = @{ ref $_[0] eq 'HASH' ? $_[0] : { @_ } }{qw(one two three) }; } Did I say clarity? HAHAHAHAHA! Surely no one would actually put something like that in his code. Except I found myself typing this very thing, and That Is Why. EXPORTS
Two functions (kw and kwn) are exported by default. You can also ask for them individually or rename them to something else. See Sub::Exporter for details. kw(@array, @names) Short for "kwn(@array, 0, @names)" kwn(@array, $number_of_positional_args, @names) Conceptually shifts off n positional arguments from array, then figures out whether the rest of the array is a list of key-value pairs or a single argument (usually, but not necessarily, a hashref). If you passed in any @names, these are used as keys into the hash, and the values at those keys are appended to any positional arguments and returned. If you do not pass @names, you will get a hashref (or whatever the single argument was, like a tied object) back. Note that if the single argument cannot be dereferenced as a hashref, this can die. No attempt is made by this module to handle the exception. AUTHOR
Paul Driver <frodwith@cpan.org> COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Paul Driver <frodwith@cpan.org>. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. perl v5.12.4 2011-01-24 Kwargs(3pm)
All times are GMT -4. The time now is 04:28 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy