我如何应用此Perl的前pression到文件的每一行? [英] How do I apply this Perl expression to each line of the file?

查看:136
本文介绍了我如何应用此Perl的前pression到文件的每一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Mac 10.9.5,bash shell中,和Perl 5,16版,颠覆3(v5.16.3)。我有以下脚本...

 #!/斌/庆典
perl的-pi -e \"s/([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?)/REPLACE INTO学生(ID,SIS_ID,STUDENT_NUM,USER_ID,OTHER_USER_ID)VALUES(REPLACE(UUID(),' - ',''),'\\ $ 24','\\ $ 26','\\ $ 2','\\ $ 27'); / G$ 1

然而,当我运行该脚本对文件...

  SH myscript.sh〜/下载/ myfile.csv

以上仅供被反对文件,而不是对文件中的每一行的第一行中运行,尽管该文件有上千行

...

  davea $厕所-l〜/下载/ myfile.csv
91552 /Users/davea/Downloads/myfile.csv

我如何调整上述使其应用于搜索和替换该文件的每一行?

编辑:这里要说的是,我传递作为输入文件的例子

  app.app.first_name,app.app.id,app.app.last_name,app.app.max_time,app.app.url,app.app.user_name,thirdparty.created,thirdparty.district,thirdparty.dob,thirdparty.ell_status,thirdparty.email,thirdparty.frl_status,thirdparty.gender,thirdparty.grade,thirdparty.hispanic_ethnicity,thirdparty.iep_status,thirdparty.last_modified,thirdparty.location.zip,thirdparty.name.first,thirdparty.name.last,thirdparty.name.middle,thirdparty.race,thirdparty.school,thirdparty.sis_id,thirdparty.state_id,thirdparty.student_number,thirdparty.id,matchmaker_result
 名字,0040FBA053464647BD51141EECF4437F,姓氏,2014年9月15日20:46:11,cityunifiedca.springboardonline.org,mlastname,2014-04-04T23:03:29.916Z,51e76ab1d93412f47b000c32,6/12/2000,,,Paid,F,10,Y,Y,2015-08-19T21:33:13.989Z,90033-1803,FIRSTNAME,LASTNAME,A,Caucasian,51f811478a86244d2900033f,061200F010,6124939964,061200F010,533f3a412a1f1fea24c8e164,match

而这里是运行以上

的输出

  REPLACE INTO学生(ID,SIS_ID,STUDENT_NUM,USER_ID,OTHER_USER_ID)VALUES(REPLACE(UUID(),' - ',''),'thirdparty.sis_id,第三方.student_number','app.app.id','thirdparty.id'); atchmaker_result
 名字,0040FBA053464647BD51141EECF4437F,姓氏,2014年9月15日20:46:11,cityunifiedca.springboardonline.org,mlastname,2014-04-04T23:03:29.916Z,51e76ab1d93412f47b000c32,6/12/2000,,,Paid,F,10,Y,Y,2015-08-19T21:33:13.989Z,90033-1803,FIRSTNAME,LASTNAME,A,Caucasian,51f811478a86244d2900033f,061200F010,6124939964,061200F010,533f3a412a1f1fea24c8e164,match


解决方案

提供的路径,输入文件作为第一个命令行参数。

注:因为我只是把你的正则表达式匹配变量和转移下来一(即我没有测试这个code)

数组索引可能会关闭

 使用严格的;
使用警告;使用文字:: CSV;我的$ = CSV文本:: CSV->新建({二进制=> 1})或死亡文本:: CSV-> error_diag;
开放式(我$跳频,'<',$ ARGV [0])或死亡$ !;而(我的$行= $ csv->函数getline($ FH)){
    打印REPLACE INTO学生(ID,SIS_ID,STUDENT_NUM,USER_ID,OTHER_USER_ID)VALUES(REPLACE(UUID(),' - ',''),'$按行> [23],$按行> 25','$按行> [1],按行$> [26]'); \\ N的;
}$ csv-> EOF或$ csv-> error_diag;
关闭($ FH);

I'm using Mac 10.9.5, bash shell, and perl 5, version 16, subversion 3 (v5.16.3). I have the following script ...

#!/bin/bash
perl -pi -e "s/([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?),([^,]+?)/REPLACE INTO student (ID, SIS_ID, STUDENT_NUM, USER_ID, OTHER_USER_ID) VALUES (REPLACE(uuid(), '-', ''), '\$24', '\$26', '\$2', '\$27');/g" $1 

However, when I run the script against a file ...

 sh myscript.sh ~/Downloads/myfile.csv

The above only gets run against the first line of the file and not against every line in the file, despite the fact the file has thousands of lines ...

davea$ wc -l ~/Downloads/myfile.csv
91552 /Users/davea/Downloads/myfile.csv

How do I adjust the above so that it applies the search and replace to every line of the file?

Edit: here is an example of a file that I'm passing in as input

 app.app.first_name,app.app.id,app.app.last_name,app.app.max_time,app.app.url,app.app.user_name,thirdparty.created,thirdparty.district,thirdparty.dob,thirdparty.ell_status,thirdparty.email,thirdparty.frl_status,thirdparty.gender,thirdparty.grade,thirdparty.hispanic_ethnicity,thirdparty.iep_status,thirdparty.last_modified,thirdparty.location.zip,thirdparty.name.first,thirdparty.name.last,thirdparty.name.middle,thirdparty.race,thirdparty.school,thirdparty.sis_id,thirdparty.state_id,thirdparty.student_number,thirdparty.id,matchmaker_result
 FirstName,0040FBA053464647BD51141EECF4437F,LastName,2014-09-15 20:46:11,cityunifiedca.springboardonline.org,mlastname,2014-04-04T23:03:29.916Z,51e76ab1d93412f47b000c32,6/12/2000,,,Paid,F,10,Y,Y,2015-08-19T21:33:13.989Z,90033-1803,FIRSTNAME,LASTNAME,A,Caucasian,51f811478a86244d2900033f,061200F010,6124939964,061200F010,533f3a412a1f1fea24c8e164,match

and here is the output of running the above

 REPLACE INTO student (ID, SIS_ID, STUDENT_NUM, USER_ID, OTHER_USER_ID) VALUES (REPLACE(uuid(), '-', ''), 'thirdparty.sis_id', 'thirdparty.student_number', 'app.app.id', 'thirdparty.id');atchmaker_result
 FirstName,0040FBA053464647BD51141EECF4437F,LastName,2014-09-15 20:46:11,cityunifiedca.springboardonline.org,mlastname,2014-04-04T23:03:29.916Z,51e76ab1d93412f47b000c32,6/12/2000,,,Paid,F,10,Y,Y,2015-08-19T21:33:13.989Z,90033-1803,FIRSTNAME,LASTNAME,A,Caucasian,51f811478a86244d2900033f,061200F010,6124939964,061200F010,533f3a412a1f1fea24c8e164,match

解决方案

Provide the path to your input file as the first command line argument.

Note: array indices might be off because I simply took your regex match variables and shifted them down by one (i.e., I didn't test this code).

use strict;
use warnings;

use Text::CSV;

my $csv = Text::CSV->new({ binary => 1 }) or die Text::CSV->error_diag;
open(my $fh, '<', $ARGV[0]) or die $!;

while (my $row = $csv->getline($fh)) {
    print "REPLACE INTO student (ID, SIS_ID, STUDENT_NUM, USER_ID, OTHER_USER_ID) VALUES (REPLACE(uuid(), '-', ''), '$row->[23]', '$row->[25]', '$row->[1]', '$row->[26]');\n";
}

$csv->eof or $csv->error_diag;
close($fh);

这篇关于我如何应用此Perl的前pression到文件的每一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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