简体   繁体   English

使用Perl命令行进行搜索和替换时遇到问题

[英]Having trouble using perl command line to do a search and replace

I'm using bash shell on Mac YOsemite. 我在Mac YOsemite上使用bash shell。 I have a file that contains lines like 我有一个包含以下行的文件

560c4f458b97330d00b0832d 5237d5eee1399d2f11005571
560c4f458b97330d00b0832e 55c4a0001b9e2d0e00032d6f
560c4f458b97330d00b0832f 55ce2e78e1055a0d008cad3c
560c4f458b97330d00b08330 55ce2e78e1055a0d008cad44
560c4f458b97330d00b08331 55ce2e78e1055a0d008cad4a
560c4f458b97330d00b08332 55c4a0001b9e2d0e00032d78
560c4f458b97330d00b08333 55c4a0001b9e2d0e00032d7b
560c4f458b97330d00b08334 55ce2e78e1055a0d008cad51

I'm trying to write an expression that will turn the file into a series of SQL statements. 我正在尝试编写一个表达式,将文件转换为一系列SQL语句。 So I tried 所以我尝试了

perl -pi -e “s/(.*?)\s(.*)/update my_sql_table set my_user_id = '$2' where my_user_id = '$1';/g" /tmp/myfile.csv

Hoping to get a file of lines line 希望得到一个文件行line

update my_sql_table set my_user_id = '5237d5eee1399d2f11005571' where my_user_id = '560c4f458b97330d00b0832d';
update my_sql_table set my_user_id = '55c4a0001b9e2d0e00032d6f' where my_user_id = '560c4f458b97330d00b0832e';

But instead the output is one massive line of statements that look like 但是相反,输出是看起来像这样的大量语句

update my_sql_table set my_user_id = '' where my_user_id = '';update my_sql_table set my_user_id = '' where my_user_id = '';update my_sql_table set my_user_id = '' where my_user_id = '';update my_sql_table set my_user_id = ''

How can I correct my above statement to put the tokens in the right place? 我该如何纠正我的上述声明以将令牌放置在正确的位置?

I'd use split instead of a regex in a perl loop: 我会在perl循环中使用split而不是正则表达式:

use strict;
use warnings;

open my $input_file, '<', 'input.txt' or die $!;

while(<$input_file>){   
        chomp;
        my @fields = split;
        print "update my_sql_table set my_user_id = $fields[1] where my_user_id = $fields[0]\n";
    }

Or as a one-liner: 还是单线:

perl -F: -lane 'print "update my_sql_table set my_user_id = $F[1] where my_user_id = $F[0]";' in.txt

Or using awk: 或使用awk:

awk '{ print "update my_sql_table set my_user_id = " $2 "where my_user_id = " $1 }' < in.txt 

A short shell loop (it seems a bit too much to pull in Perl to do this): 一个简短的shell循环(为了执行此操作,似乎不太适合使用Perl):

while read -r old new; do
    cat <<SQL_END
UPDATE my_sql_table
    SET my_user_id   = '$new'
    WHERE my_user_id = '$old';
SQL_END
done

Running it with the provided data in data.in : 使用data.in提供的数据运行它:

$ sh scripts.sh <data.in
UPDATE my_sql_table
    SET my_user_id   = '5237d5eee1399d2f11005571'
    WHERE my_user_id = '560c4f458b97330d00b0832d';
UPDATE my_sql_table
    SET my_user_id   = '55c4a0001b9e2d0e00032d6f'
    WHERE my_user_id = '560c4f458b97330d00b0832e';
UPDATE my_sql_table
    SET my_user_id   = '55ce2e78e1055a0d008cad3c'
    WHERE my_user_id = '560c4f458b97330d00b0832f';
UPDATE my_sql_table
    SET my_user_id   = '55ce2e78e1055a0d008cad44'
    WHERE my_user_id = '560c4f458b97330d00b08330';
UPDATE my_sql_table
    SET my_user_id   = '55ce2e78e1055a0d008cad4a'
    WHERE my_user_id = '560c4f458b97330d00b08331';
UPDATE my_sql_table
    SET my_user_id   = '55c4a0001b9e2d0e00032d78'
    WHERE my_user_id = '560c4f458b97330d00b08332';
UPDATE my_sql_table
    SET my_user_id   = '55c4a0001b9e2d0e00032d7b'
    WHERE my_user_id = '560c4f458b97330d00b08333';
UPDATE my_sql_table
    SET my_user_id   = '55ce2e78e1055a0d008cad51'
    WHERE my_user_id = '560c4f458b97330d00b08334';

This is simple using Perl's command-line autosplit option 使用Perl的命令行自动拆分选项很简单

perl -anE'print qq{update my_sql_table set my_user_id = \'$F[1]\' where my_user_id = \'$F[0]\';\n}' ids.txt

output 输出

update my_sql_table set my_user_id = '5237d5eee1399d2f11005571' where my_user_id = '560c4f458b97330d00b0832d';
update my_sql_table set my_user_id = '55c4a0001b9e2d0e00032d6f' where my_user_id = '560c4f458b97330d00b0832e';
update my_sql_table set my_user_id = '55ce2e78e1055a0d008cad3c' where my_user_id = '560c4f458b97330d00b0832f';
update my_sql_table set my_user_id = '55ce2e78e1055a0d008cad44' where my_user_id = '560c4f458b97330d00b08330';
update my_sql_table set my_user_id = '55ce2e78e1055a0d008cad4a' where my_user_id = '560c4f458b97330d00b08331';
update my_sql_table set my_user_id = '55c4a0001b9e2d0e00032d78' where my_user_id = '560c4f458b97330d00b08332';
update my_sql_table set my_user_id = '55c4a0001b9e2d0e00032d7b' where my_user_id = '560c4f458b97330d00b08333';
update my_sql_table set my_user_id = '55ce2e78e1055a0d008cad51' where my_user_id = '560c4f458b97330d00b08334';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM