How to match the below conditions?

Hello All,

I have 2 files with around 2k records.. I am lost how to build a script to create another file with the below:confused::eek:

Match Condition1:
File1's 12th Columns data should exact match with File2's 19th Column's data

Match Condition2:
File1's 28th Column's data format is like "RSL N" this must match with File2's 29th Column's data Formant "TRE N".. Match must be in terms of N.. While N is in numbers..

Script must find when the Match condition1 is TRUE AND Match condition2 is FALSE & then create a file with the below format:

"Match condition1;File1's 28th Column's data"

What is the field separator?

You could use Perl to create a hashmap of file 1 indexed on field 12, then, go through file 2 looking at whether field 28 of the relevant hashmap entry matches field 29 of the current record...

It is ";"

Sorry I should mentioned it already..:frowning:

So something like the following WARNING UNTESTED OFF THE TOP OF MY HEAD CODE FOLLOWS:

perl -ne 'BEGIN {open file1_fh, "<", "/path/to/file1";
  while(<file1_fh>){@r=split/;/,$_;
    push @{$records{$r[11]}},$_;
  }
}
@r=split/;/,$_;
for $record (@{$records{$r[18]}){
  @f=split/;/,$record;
  print   "$r[18];RSl $1\n" if (($f[27]=~/RSL\s+(\d+)/)&&($r[28] eq "TRE $1"));
}' /path/to/file_2 >outputfile
1 Like

Thanks Skrynesaver..

I have other part of the main script which is written in a shell script.. Actually this is a part of a big shell script..:o

Can you please help me doing this job in bash script?:smiley:

You can call Perl within a bash script, just as you can call sed or awk, the example above, using the same quotes could be dropped into a script as a single command. (Though a few comments might be good form :wink:

1 Like

Try

awk     'NR==FNR        {sub (/RSL /, "", $28)
                         T[$12]=$28
                         next 
                        }
         $19 in T       {sub (/TRE /, "", $29)
                         if ($29 == T[$19]) print $19, T[$19]
                        }   
        ' FS=";" file1 file2

Untested as you didn't supply input nor output samples...

Getting below errors:

awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 5
awk: bailing out near line 5

Quoting Don Cragun:

1 Like

Thanks AWK worked :smiley:

Thanks RudiC.. Seems I need to remember this..:o

But I am afraid why it returns nothing while I know atleast a case exists as below:confused::

File1 contains below Column # 12 & Column # 28

{{ bsmID { amecID 10, moiRdn 1}, moiRdn 3}};RSL 12

& File2 does not contain Column # 19 & Column # 29 as expected

{{ bsmID { amecID 10, moiRdn 1}, moiRdn 3}};tre 12 

---------- Post updated at 05:50 PM ---------- Previous update was at 05:46 PM ----------

Sample files are in my previous post

http://www.unix.com/302931599-post8.html

As I said: untested because no sample file available. So the tests are up to you.

And, what do you mean with "...File2 does not contain Column # 19..."? Does it or doesn't it?
And, your request was for "TRE" in col 29, above you post "tre".

To me, it'd be normal and logical that no match is found.

Now I run this (Note: I changed TRE to tre)..

/usr/xpg4/bin/awk     'NR==FNR        {sub (/RSL /, "", $28)
                         T[$12]=$28
                         next
                        }
         $19 in T       {sub (/tre /, "", $29)
                         if ($29 == T[$19]) print $19, T[$19]
                        }
        ' FS=";" file1 file2

This time it is not blank.. there few lines but not the same as I expected..

Here is one example what should have happened.

File1 contains below Column # 12 & Column # 28


{{ bsmID { amecID 10, moiRdn 1}, moiRdn 3}};RSL 12

& File2 DOES NOT contain Column # 19 & Column # 29 as expected


{{ bsmID { amecID 10, moiRdn 1}, moiRdn 3}};tre 12

The script was expected to print


{{ bsmID { amecID 10, moiRdn 1}, moiRdn 3}};RSL 12

Try

awk     'NR==FNR        {T[$12]=$28
                         next 
                        }
         $19 in T       {sub (/tre/, "RSL", $29)
                         if ($29 != T[$19]) print $19, T[$19]
                        }   
        ' FS=";" OFS=";" /tmp/file1.txt /tmp/file2.txt
1 Like