473,769 Members | 3,455 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 #1
10 2249
eWish
971 Recognized Expert Contributor
Is this school/class work? What have you tried so far?

Here is the best place to start. They have a wonderful search feature. If you have further problems once you have written your code, then post it here and we will be glad to assist you. Please use the code tags when posting code samples.

--Kevin
Aug 4 '08 #2
KevinADC
4,059 Recognized Expert Specialist
ask over on www.unix.com in the Shell Programming and Scripting forum.
Aug 4 '08 #3
masinick
7 New Member
Is this school/class work? What have you tried so far?

Here is the best place to start. They have a wonderful search feature. If you have further problems once you have written your code, then post it here and we will be glad to assist you. Please use the code tags when posting code samples.

--Kevin
No, this is not school work, it is for a task on a job. I am a project manager now so my coding skills have become stale.

I'll see if I can find anything helpful at perldoc.perl.or g, but I have been hunting around since Friday without much success, plus I have been trying to digest stuff from the Perl Cookbook and Programming Perl, and I have been confusing myself.
Aug 4 '08 #4
masinick
7 New Member
ask over on www.unix.com in the Shell Programming and Scripting forum.
I asked over at unix.com on Friday and have not had any responses at all.
Aug 4 '08 #5
masinick
7 New Member
I put the following code in my script and I can now reduce the search on the first file to records containing |Y|.

I still need to compare this reduced file to a second file, taking the second field in the first file and comparing it to the fourth field in the second file, then output matching records into another file using the first, second, and fourth fields from the second file.

Any suggestions on how to do this?

Here is what I came up with to locate the |Y| in the first file:
Expand|Select|Wrap|Line Numbers
  1. while (<MINPUT>) {
  2.    if ( /\|Y\|/ ) {
  3.       print MOUTPUT;
  4.       my $line = <SINPUT>;
  5.    }
  6. }
Aug 4 '08 #6
KevinADC
4,059 Recognized Expert Specialist
I put the following code in my script and I can now reduce the search on the first file to records containing |Y|.

I still need to compare this reduced file to a second file, taking the second field in the first file and comparing it to the fourth field in the second file, then output matching records into another file using the first, second, and fourth fields from the second file.

Any suggestions on how to do this?

Here is what I came up with to locate the |Y| in the first file:
Expand|Select|Wrap|Line Numbers
  1. while (<MINPUT>) {
  2.    if ( /\|Y\|/ ) {
  3.       print MOUTPUT;
  4.       my $line = <SINPUT>;
  5.    }
  6. }

Your code does not make sense. You are looping through <MINPUT> , but you assigning a value to $line from <SINPUT>, and since it is scpoed only to that very small block of code it can't be used for anything else.

Also, if the pattern |Y| can be in another part of the line you might ger false matches. Maybe you should split the input file and check the specific column for a 'Y' instead of pattern matching the entire line for a substring.

Post some sample data you are working with, explain it if necessary.
Aug 4 '08 #7
masinick
7 New Member
I should take the SINPUT line out of there; it does nothing, but the rest of that loop actually works and it identifies lines containing |Y|, which is a unique pattern. I output the lines containing this match to a file, and I now want to compare that file to another file which has stuff that looks like this:

1460151|Duke,Jo hn|6021|a000822 |SKYWRITER

It is the a000822 that I want to match in this second file with identical entries in the second field of the first file.

So what I would want to do is open the first file, read a record and set a variable to the second pipe delimited field, then read a record in the second file, examine the fourth field and look for an entry that matches the second field in that first file.

The first file looks like:

A000822|a000822 |<lots of other fields that I do not care about - 34 in all>
Aug 4 '08 #8
masinick
7 New Member
The sed replacement code now looks like this:

Expand|Select|Wrap|Line Numbers
  1.       while (<MINPUT>) {
  2.          if ( /\|Y\|/ ) {
  3.             print MOUTPUT;
  4.          }
  5.       }
Aug 4 '08 #9
KevinADC
4,059 Recognized Expert Specialist
How big are the files?
Aug 4 '08 #10

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
4109
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
4062
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
4802
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
9745
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
3223
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
9578
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...
0
9413
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10192
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9848
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...
1
7391
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
5429
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3941
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
2
3544
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2810
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.