How to match the below conditions?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to match the below conditions?
# 1  
Old 01-12-2015
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 belowSmilieSmilie

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"
# 2  
Old 01-12-2015
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...
# 3  
Old 01-12-2015
It is ";"

Sorry I should mentioned it already..Smilie
# 4  
Old 01-12-2015
So something like the following WARNING UNTESTED OFF THE TOP OF MY HEAD CODE FOLLOWS:
Code:
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

This User Gave Thanks to Skrynesaver For This Post:
# 5  
Old 01-13-2015
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..Smilie

Can you please help me doing this job in bash script?Smilie
# 6  
Old 01-13-2015
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 Smilie
This User Gave Thanks to Skrynesaver For This Post:
# 7  
Old 01-13-2015
Try
Code:
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...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Data match 2 files based on first 2 columns matching only and join if match

Hi, i have 2 files , the data i need to match is in masterfile and i need to pull out column 3 from master if column 1 and 2 match and output entire row to new file I have tried with join and awk and i keep getting blank outputs or same file is there an easier way than what i am... (4 Replies)
Discussion started by: axis88
4 Replies

2. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Shell Programming and Scripting

awk to print fields that match using conditions and a default value for non-matching in two files

Trying to use awk to match the contents of each line in file1 with $5 in file2. Both files are tab-delimited and there may be a space or special character in the name being matched in file2, for example in file1 the name is BRCA1 but in file2 the name is BRCA 1 or in file1 name is BCR but in file2... (6 Replies)
Discussion started by: cmccabe
6 Replies

4. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

5. Shell Programming and Scripting

awk to match field between two files and use conditions on match

I am trying to look for $2 of file1 (skipping the header) in $2 of file2 (skipping the header) and if they match and the value in $10 is > 30 and $11 is > 49, then print the line from file1 to a output file. If no match is foung the line is not printed. Both the input and output are tab-delimited.... (3 Replies)
Discussion started by: cmccabe
3 Replies

6. Shell Programming and Scripting

Display match or no match and write a text file to a directory

The below bash connects to a site, downloads a file, searches that file based of user input - could be multiple (all that seems to work). What I am not able to figure out is how to display on the screen match found or no match found" and write a file to a directory (C:\Users\cmccabe\Desktop\wget)... (4 Replies)
Discussion started by: cmccabe
4 Replies

7. Shell Programming and Scripting

Match pattern1 in file, match pattern2, substitute value1 in line

not getting anywhere with this an xml file contains multiple clients set up with same tags, different values. I need to parse the file for client foo, and change the value of tag "64bit" from false to true. cat clients.xml <Client type"FIX"> <ClientName>foo</ClientName>... (3 Replies)
Discussion started by: jack.bauer
3 Replies

8. Shell Programming and Scripting

Errors in if conditions with to many OR conditions

Hi ALL I have a script where in i need to check for several values in if conditons but when i execute the script it throws error such as "TOO MANY ARGUMENTS" if then msg="BM VAR Issue :: bmaRequestVAR=$bmaRequestVAR , nltBMVAR=$nltBMVAR , bmaResponseVAR=$bmaResponseVAR ,... (10 Replies)
Discussion started by: nikhil jain
10 Replies

9. UNIX for Dummies Questions & Answers

move files that match specific conditions

Hi all, I'm new to this forum and bash scripting. I have the following problem, I need to copy some files (from one dir. to another) whose first 5 numbers (subjects' ID) match the directory names. Here a shortened version of what I'm trying to do: names=(32983_f 35416_f 43579_f) # these are... (6 Replies)
Discussion started by: ada1983
6 Replies

10. UNIX for Dummies Questions & Answers

Lynx Grep Pattern Match 2 conditions Print from Start to End

I am working on a scraping project and I am stuck at this tiny grep pattern match. Sample text : FPA List. FPA List. FPA List. FPA List. FPA List. FPA List. FPA List. FPA List. ABC Personal Planning Catherine K. Wat Cath Wat Catherine K. Wat Catherine K. Wat IFRAME:... (8 Replies)
Discussion started by: kkiran
8 Replies
Login or Register to Ask a Question