sed搜索的Perl语法 [英] Perl syntax for sed searches

查看:58
本文介绍了sed搜索的Perl语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Perl有许多最初来自sed和awk的功能。我有一个我正在使用的模式:


sed -n''/ | Y | / p''


我想要在Perl中做同样的事情,并能够将该值保存在某种变量或数组中,或者可能将其写入文件。


对于简单的情况,写出来对于文件,我认为语法非常接近sed语法。我想先得到一些建议,首先是在Perl中编写类似表达式的几种替代方法,然后如何正确地进行I / O.


我的第二个问题是我有两个文件,都有管道分隔数据。在第一个文件中,我想首先进行大数据缩减,采用上面的模式,并仅保留包含| Y |的记录。在第二个文件中,我有一个包含员工编号的字段,其中A为第一个数字。另一个较大的文件包含相同的数据,但是使用小写字母a。


然后,完整的练习是首先将第一个文件减少到字段中包含Y的记录,包围通过管道符号。接下来,在确保两个文件中的记录较低之后,我需要将第二个文件中匹配的记录与员工ID字段进行比较。我需要在匹配的记录中打印两个,可能是三个字段,可能是字段1,3和4.


任何人都可以给我一些关键的技术片段,所以我我不会继续挣扎,然后我会将这项技术应用到我在Perl中编写的适度大小的应用程序 - 以提高速度和便携性。我之前使用过Perl,但我从未成为专家,自从我最后使用它已经有好几年了。我用一些不同的语法混淆了自己并犯了很多愚蠢的错误,所以我很感激一些合理的建议让我回到正轨。我正在阅读一些经典着作 - Perl Cookbook和Programming Perl,但两者都是大书,令人望而却步。在我能够消化它们之前,我会理解一些指示来加速我的学习,更重要的是,尽快获得至少一个最低限度可用形式的脚本。因此,我很感激具体的提示。一旦我消化了经典资源并实际做了更多编码以重新获得经验,我会变得更好。

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.

推荐答案

这个学校/班级是否有效?你到目前为止尝试了什么?


这是最好的起点。他们有一个很棒的搜索功能。如果您在编写代码后还有其他问题,请在此处发布,我们很乐意为您提供帮助。发布代码示例时请使用代码标签。


--Kevin
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


在Shell编程和脚本编写www.unix.com上询问论坛。
ask over on www.unix.com in the Shell Programming and Scripting forum.



这个学校/班级有用吗?你到目前为止尝试了什么?


这是最好的起点。他们有一个很棒的搜索功能。如果您在编写代码后还有其他问题,请在此处发布,我们很乐意为您提供帮助。发布代码示例时请使用代码标签。


--Kevin
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



不,这不是学校工作,它是为了工作的任务。我现在是项目经理,所以我的编码技巧已经过时了。


我会看到我能在perldoc.perl.org找到任何有用的东西,但我一直在寻找从星期五开始没有太大的成功,加上我一直试图从Perl Cookbook和Programming Perl中消化东西,而且我一直在困惑自己。

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.org, 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.


这篇关于sed搜索的Perl语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆