Perl脚本复制行以放置在新文件中 [英] Perl script to copy rows to place in new file

查看:64
本文介绍了Perl脚本复制行以放置在新文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件夹中有很多文本文件.文件夹为"c:\ vehicles".对于每个文本文件,我想复制包含以下单词的任何行:model,make,year.我要写入的文件是"vehicles.txt",位于"c:\"中.

I have a lot of text files in a folder. The folder is 'c:\vehicles'. For every text file, I want to copy any row that includes the words: model, make, year. The file I want to write to is 'vehicles.txt' and located in 'c:\'.

我知道我写错了代码.我该怎么做才能纠正它?谢谢您的帮助.

I know I've written the code wrong. What should I do to correct it? Thanks for the help.

C:\ vehicles $ ls -A |xargs head -qn 30 |perl -Mstrict -wne'if($ + _ =〜/(make)|(model)|(year)/){打印"$ _";}'> Vehicles.txtgrep -rE(make | model | year)" c:

推荐答案

以下内容可能会有所帮助:

Perhaps the following will help:

use strict;
use warnings;

for my $file (<*.txt>) {
    open my $fh, '<', $file or die $!;

    while (<$fh>) {
        chomp;
        print "$_\n" if /(?:\b(?:model|make|year)\b)/i;
    }
    close $fh;
}

假设脚本将位于 c:\ vehicles 中,请在命令提示符下键入 perl scriptName.pl> vehicles.txt .< *.txt> 表示法返回目录中所有文本文件的列表.这些文件中的每一个都是逐行打开和读取的.如果在一行中找到您要查找的任何单词,则会将其打印出来.> vehicles.txt 表示要打印到文件.

Assuming the script will be in c:\vehicles, type perl scriptName.pl >vehicles.txt at the command prompt. The <*.txt> notation returns a list of all text files in the directory. Each of these files are opened and read, line by line. If any of the words your looking for are found on a line, it's printed. The >vehicles.txt notation means to print to the file.

这篇关于Perl脚本复制行以放置在新文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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