473,776 Members | 2,054 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Perl syntax for sed searches

7 New Member
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. Next, I need to compare the records that match in the second file to the employee ID field, after making sure it is lower cased in both files. I will need to print out two, possibly three fields within records that match, perhaps fields 1, 3, and 4.

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.
Aug 4 '08
10 2252
masinick
7 New Member
CLOSED: Program written, thank you very much!
Here is the final version (edited) of what I came up with:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl 
  2.  
  3. # File: MatchID.pl
  4. # Author: Brian Masinick
  5. # Initial Creation Date: August 1, 2008
  6.  
  7.  
  8. # Inputs:
  9. # M input feed file
  10. # S input feed file
  11.  
  12. # Intermediate outputs:
  13. # M output feed matching |Y|
  14.  
  15. # Final outputs:
  16. # S output file matching records in M output file with those in S input file.
  17.  
  18. # Required software: Perl 5.10
  19.  
  20. open (MINPUT,"<M_input_file") || die("Could not open M_input_file");
  21. open (MOUTPUT,">M_output_file") || die("Could not open M_output_file");
  22. open (SOUTPUT,">Sl_output_file") || die("Could not open S_output_file");
  23.  
  24. # This produces the intermediate file, so we have a written record of which set
  25. # of records we are actually processing to produce the final result.
  26.  
  27. while (<MINPUT>) {
  28.    if ( /\|Y\|/ ) {
  29.       print MOUTPUT;
  30.    }
  31. }
  32.  
  33. close (MINPUT,  M_input_file);
  34. close (MOUTPUT, M_output_file);
  35.  
  36. print "M registered users recorded.\n";
  37.  
  38. open (MOUTPUT,"<M_output_file") || die("Could not open M_output_file");
  39.  
  40. while (<MOUTPUT>) {
  41.         # split the input line into the @fields array
  42.         @fields = split "[|]";
  43.         # store the line in the %moutput hash indexed by the second field
  44.         $moutput{$fields[1]}=$_;
  45. }
  46.  
  47.  
  48. open (SINPUT,"<S_input_file") || die("Could not open S_input_file");
  49.  
  50. print "Reading S users file.\n";
  51.  
  52. while (<SINPUT>) {
  53.         # split the input line into the @fields array
  54.         @fields=split "[|]";
  55.         # if the fourth field exists in the %moutput hash, print the current
  56.         # input line
  57.         if (exists $moutput{$fields[3]}) { print SOUTPUT}
  58. }
  59.  
  60. print "Processing complete.\n";
  61.  
  62. close (MOUTPUT, $MIIS_output_file);
  63. close (SINPUT,  $Skytel_input_file);
  64. close (SOUTPUT, $Skytel_output_file);
  65.  
  66. print "Files closed. Program complete.\n";
  67.  
  68.  
Aug 5 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1717
by: Cameron Laird | last post by:
In article <iL3jc.232$Yc.2330@news4.e.nsc.no>, Leif B. Kristensen <junkmail@solumslekt.org> wrote:
42
4110
by: Fred Ma | last post by:
Hello, This is not a troll posting, and I've refrained from asking because I've seen similar threads get all nitter-nattery. But I really want to make a decision on how best to invest my time. I'm not interested on which language is better in *general*, just for my purpose. My area of research is in CAD algorithms, and I'm sensing the need to resort to something more expedient than C++, bash scripting, or sed scripting.
77
4065
by: Hunn E. Balsiche | last post by:
in term of its OO features, syntax consistencies, ease of use, and their development progress. I have not use python but heard about it quite often; and ruby, is it mature enough to be use for developing serious application, e.g web application as it has not many features in it yet. I've given up on Perl for its ugly syntax and it is not the easiest language to learn. How about PHP? Thanks
4
1470
by: Xah Lee | last post by:
while programing in Python, one can lookup syntax or info for keywords or modules within Python. In the command line, type python to get into the python interactive program. then type help() >From there one can type any keyword or module name to find out the
31
4807
by: surfunbear | last post by:
I've read some posts on Perl versus Python and studied a bit of my Python book. I'm a software engineer, familiar with C++ objected oriented development, but have been using Perl because it is great for pattern matching, text processing, and automated testing. Our company is really fixated on risk managnemt and the only way I can do enough testing without working overtime (which some people have ended up doing) is by automating my...
29
2477
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules" for when and how to use single quotes and double quotes in ASP? thanks! ---------------------- SQL = SQL & "WHERE '" & REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE("GenKTitles.
0
9747
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile. I need it to be compiled with threads. Anyone have any wisdom on how best to do this? Here's a transcript of my latest attempt. It's long; you might want to skip to the bottom, where I try "make" and the fatal errors start happening.
5
3226
by: oaktown | last post by:
Running two input forms on the same page, one php and the other (the one I would like to create) to be written in perl. The idea is to accept an input from a perl script, then pass that input to two other input fields, one in perl and the other in php. The two existing input fields (perl and php) are stand alone scripts. The perl script searches a flat text data base, while the php script searches a MySQL data base. The end result desired is to...
11
1596
by: Ben | last post by:
Here are the statistics from Google Trends: http://benyang22a.blogspot.com/2007/09/perl-vs-python.html
0
9625
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10056
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9920
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8948
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7467
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6721
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5489
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4027
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2857
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.