Perl syntax for sed searches


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl syntax for sed searches
# 8  
Old 08-04-2008
Quote:
Originally Posted by Annihilannic
You use $ to refer to individual elements of an array, @ is for referring to the entire array.

Those nested while loops would be quite inefficient because you would be reading the entire SINPUT file for every iteration of the outside loop. I would read in the MOUTPUT file first and load it into a hash, using something like:

Code:
while (<MOUTPUT>) { @fields = split "[|]"; $moutput{@fields[1]}=$_; };

Then process the second file checking against that %moutput hash:

Code:
while (<SINPUT>) { @fields=split "[|]"; if (exists $moutput{$fields[3]}) { print $moutput{$fields[3]}; } ;

(all code untested!)

Obviously you will have a little more work to do to only print out the fields you are interested in, but you get my drift.
How about this then?

Code:
while (<MOUTPUT>) { @fields1 = split "[|]"; $moutput{@fields1[1]}=$_; };

and then

Code:
while (<SINPUT>) { @fields2=split "[|]"; if ($moutput{$fields1[1] eq $moutput{$fields2[3]) { print $moutput{$fields2}; } ;

because what I really want is to match the second field in file 1 with the fourth field in file 2, and if so, print the entire record for that matching field from file 2. Would my code do that?
# 9  
Old 08-04-2008
No, because the contents of @fields1 are overwritten as each line of the MOUTPUT file is processed, so it only contains the last line by the time you are processing the second loop.

My code should do what you describe, if I didn't screw something up?
# 10  
Old 08-04-2008
Quote:
Originally Posted by Annihilannic
No, because the contents of @fields1 are overwritten as each line of the MOUTPUT file is processed, so it only contains the last line by the time you are processing the second loop.

My code should do what you describe, if I didn't screw something up?
So your code would compare the second field in the first file with the fourth field in the second file, and if they match, print out the full record from the second file?

I don't understand the syntax then. Could you explain the algorithm to me, breaking it down line by line? I sure appreciate all of your help. Assuming the code is correct, it is a very elegant solution, but I need to understand it a lot better. I am not getting it right now, so all of your help is greatly appreciated! Thanks!
# 11  
Old 08-05-2008
Here it is, tested, and with comments.

Code:
$ cat moutput
1|1|3|4|5|6|7
1|2|3|4|5|6|7
1|3|3|4|5|6|7
1|4|3|4|5|6|7
$ cat sinput
1|2|3|2|5|6
1|2|3|4|5|6
1|2|3|5|5|6
1|2|3|7|5|6
$ perl masinick
1|2|3|2|5|6
1|2|3|4|5|6
$ cat masinick
#!/usr/bin/perl -w
open MOUTPUT,"< moutput";
while (<MOUTPUT>) {
        # split the input line into the @fields array
        @fields = split "[|]";
        # store the line in the %moutput hash indexed by the second field
        $moutput{$fields[1]}=$_;
}
open SINPUT,"< sinput";
while (<SINPUT>) {
        # split the input line into the @fields array
        @fields=split "[|]";
        # if the fourth field exists in the %moutput hash, print the current
        # input line
        if (exists $moutput{$fields[3]}) { print }
}
$

As it stands it only uses the index of the %moutput hash, not the data stored in it, but I'm presuming you'll need that at some point to pull out only the fields you are interested in from the first file.
# 12  
Old 08-05-2008
CLOSED: Program written, thank you very much!

Here is the final version (edited) of what I came up with:

Code:
#!/usr/bin/perl 

# File: MatchID.pl
# Author: Brian Masinick
# Initial Creation Date: August 1, 2008


# Inputs:
# M input feed file
# S input feed file

# Intermediate outputs:
# M output feed matching |Y|

# Final outputs:
# S output file matching records in M output file with those in S input file.

# Required software: Perl 5.10

open (MINPUT,"<M_input_file") || die("Could not open M_input_file");
open (MOUTPUT,">M_output_file") || die("Could not open M_output_file");
open (SOUTPUT,">Sl_output_file") || die("Could not open S_output_file");

# This produces the intermediate file, so we have a written record of which set
# of records we are actually processing to produce the final result.

while (<MINPUT>) {
   if ( /\|Y\|/ ) {
      print MOUTPUT;
   }
}

close (MINPUT,  M_input_file);
close (MOUTPUT, M_output_file);

print "M registered users recorded.\n";

open (MOUTPUT,"<M_output_file") || die("Could not open M_output_file");

while (<MOUTPUT>) {
        # split the input line into the @fields array
        @fields = split "[|]";
        # store the line in the %moutput hash indexed by the second field
        $moutput{$fields[1]}=$_;
}


open (SINPUT,"<S_input_file") || die("Could not open S_input_file");

print "Reading S users file.\n";

while (<SINPUT>) {
        # split the input line into the @fields array
        @fields=split "[|]";
        # if the fourth field exists in the %moutput hash, print the current
        # input line
        if (exists $moutput{$fields[3]}) { print SOUTPUT}
}

print "Processing complete.\n";

close (MOUTPUT, M_output_file);
close (SINPUT,  S_input_file);
close (SOUTPUT, S_output_file);

print "Files closed. Program complete.\n";


Last edited by masinick; 08-05-2008 at 12:12 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

2. Shell Programming and Scripting

Rewrite sed to perl or run sed in perl

I am having trouble re-writing this sed code sed -nr 's/.*del(+)ins(+).*NC_0{4}(+).*g\.(+)_(+).*/\3\t\4\t\5\t\1\t\2/p' C:/Users/cmccabe/Desktop/Python27/out_position.txt > C:/Users/cmccabe/Desktop/Python27/out_parse.txt in perl Basically, what the code does is parse text from two fields... (12 Replies)
Discussion started by: cmccabe
12 Replies

3. Shell Programming and Scripting

Perl/sed Escape Syntax Problem . . .

Greetings! I've run into this before; and am having a spot of trouble trying to figure out the way that Perl would prefer the following example syntax to be formed:#!/usr/bin/perl use strict; use warnings; use diagnostics; `sed -i 's/Hi Mom!\|Hi Dad!/Bye Everyone!/I' ./test.txt`;Perl... (5 Replies)
Discussion started by: LinQ
5 Replies

4. UNIX for Dummies Questions & Answers

Perl syntax

Query with perl syntax Aim: is to change a perl script to use a new file I was required to replace - entries \"$entries\" with -lib <full_path_to_filename> So in the code detector.pm sub rundetector { my $class = shift; mkdir($resultDirectory); my... (3 Replies)
Discussion started by: sa@@
3 Replies

5. Shell Programming and Scripting

Perl syntax

I'm a newbie to perl scripting. Can someone please explain the syntax below. Specifically what does -> and => do? $tz->site( => $site); (10 Replies)
Discussion started by: scj2012
10 Replies

6. Shell Programming and Scripting

sed s/// syntax help

<tr><td width=10% style='width:5%;background:#F7F0D9;padding:0in 0in 0in 0in 0in'><center><b>Package</b></td><td width=10% valign=center style='width:5%;background:#F7F0D9;padding:0in 0in 0in 0in 0in'><center><b>JTs</b></td> This is got to be simple. I run this on the above .html file: sed... (8 Replies)
Discussion started by: dba_frog
8 Replies

7. UNIX for Dummies Questions & Answers

sed - need help for syntax

Hello, here is what I've got : FILE='/OPERATIONNEL/SATURNE/CHAMPS/MASTER/ANA/SATURNE_1DAV_20080805_20080806_S3D_T_R20080806.NC ';;... (4 Replies)
Discussion started by: Aswex
4 Replies

8. Shell Programming and Scripting

perl syntax help

Im new at scripting and im trying to write a script using perl that will make sure there are 2 command line integer arguments and then check if the 2nd argument is greater than the first. i believe im close but still receive my error message even when i have 2 arguments and the second part gives me... (6 Replies)
Discussion started by: livewire06
6 Replies

9. Shell Programming and Scripting

What syntax to use with sed c\

I know that I want to entirely replace line 3 in my file filename.txt. I have tried all sorts of variations of sed 3,3,c\replacement stuff\ filename.txt with no success. about the only thing that causes any reaction is sed 3,3c\\ filename.txt but it just prints out the whole file. ... (13 Replies)
Discussion started by: SusanDAC
13 Replies

10. UNIX for Dummies Questions & Answers

sed syntax

Hi, How can i use sed command to modify a part of a variable containing "/" by another containing "/" like describe below: VAR="/app/share/eai" VAR1="/app/share" VAR2="/data/test" echo $VAR | sed 's/... ??? # using sed to replace $VAR1 in $VAR by $VAR2 ? (4 Replies)
Discussion started by: jo_aze
4 Replies
Login or Register to Ask a Question