Perl syntax for sed searches


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl syntax for sed searches
# 1  
Old 08-01-2008
Perl syntax for sed searches

I am aware that Perl has a lot of features that originally came from sed and awk. I have a pattern that I am using like this:

sed -n '/|Y|/p'

I want to do the same thing in Perl and be able to either save that value in some kind of variable or array or potentially write it out to a file.

For the simple case, writing out to a file, I think the syntax is very close to the sed syntax. I would like to get a few recommendations, first, on a few alternative ways to write a similar expression in Perl, then how to do I/O properly.

My second question is that I have two files, both with pipe separated data. In the first file, I want to do a large data reduction first, taking the pattern above, and retaining only records containing |Y|. In the second file, I have a field containing an employee number with an A as the first digit. The other larger file contains this same data, but with a lower case a.

The complete exercise, then, is to first reduce the first file to records containing Y in the field, surrounded by the pipe symbol. Then compare the records that match in the second file to the employee ID field, after making sure it is lower cased in both files.

Can anyone give me a few key technology snippets on this so I don't keep struggling with it, and I will then apply that technology to my modest sized application, which I am writing in Perl - for speed and portability. I have used Perl before, but I have never become an expert, and it has been years since I used it last. I am confusing myself with pieces of different syntax and making a lot of silly mistakes, therefore I would appreciate some sound advice to set me back on course. I am reading up using a few classics - the Perl Cookbook and Programming Perl, but both are large books and daunting to get through. Until I can digest them, I'd appreciate some pointers to accelerate my learning, and more importantly, get a script in at least a minimally usable form ASAP. Therefore, I appreciate specific tips. I'll get better at it once I have digested the classic resources and actually done more coding to regain the experience.
# 2  
Old 08-04-2008
Need to compare two files

I got the first part of my question answered. Here is a code snip for that part:

Code:
while (<MINPUT>) {
    if ( /\|Y\|/ ) {
        print MOUTPUT;
        my $line = <SINPUT>;
    }
}

On the second part, the part I am having trouble with, now that I have matched records in the first file containing |Y|, I want to compare records in the first file with the second file, taking the second field in the first file and the fourth field in the second file. When I find a match, I want to output the first, second, and fourth fields in the second file to a third output file, which will be the resulting output of the comparisons.

Any suggestions on how to do this please?
# 3  
Old 08-04-2008
Have a look at the perlrun man page, specifically the -n option. This would allow you to write the above script as a shorter one-liner since perl looks after the while (<>) part for you. Probably not relevant since you intend to write a more complex script anyway, but useful to know.

For the second part, I would load the first file into a hash indexed by the comparison field, then read through the second file, and for any record where the fourth field exists in your previously populated hash, output the wanted fields.
# 4  
Old 08-04-2008
The line my $line = <SINPUT>; is not needed. Was originally going to include that to work on the second file.

I still need to work on that part.

I am thinking of using split to pick off the second field in the first file and the fourth field in the second file. I have not gotten the syntax right yet though.


Quote:
Originally Posted by masinick
I got the first part of my question answered. Here is a code snip for that part:

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

On the second part, the part I am having trouble with, now that I have matched records in the first file containing |Y|, I want to compare records in the first file with the second file, taking the second field in the first file and the fourth field in the second file. When I find a match, I want to output the first, second, and fourth fields in the second file to a third output file, which will be the resulting output of the comparisons.

Any suggestions on how to do this please?
# 5  
Old 08-04-2008
If you use this:

Code:
@fields=split "[|]";

It will split the current record (i.e. $_) into fields and assign it to the @fields array, which you can access using $fields[0], $fields[1], etc.
# 6  
Old 08-04-2008
OK, good. Zero offset arrays. I can deal with that.

Would the comparisons then be something like this?

Assuming that I do:

Code:
@fields1=split "[|]";
@fields2=split "[|]";

Code:
if (@fields1[1] eq @fields[3])  {
   print @fields2;
}

or is the syntax something different? Is there a convenient way to take the input and put it straight into those fields, something like:

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

or does it work a different way?
# 7  
Old 08-04-2008
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.
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