无法使用perl命令行进行搜索和替换

时间:2016-07-26 15:19:41

标签: regex bash perl search replace

我在Mac YOsemite上使用bash shell。我有一个包含

等行的文件
560c4f458b97330d00b0832d 5237d5eee1399d2f11005571
560c4f458b97330d00b0832e 55c4a0001b9e2d0e00032d6f
560c4f458b97330d00b0832f 55ce2e78e1055a0d008cad3c
560c4f458b97330d00b08330 55ce2e78e1055a0d008cad44
560c4f458b97330d00b08331 55ce2e78e1055a0d008cad4a
560c4f458b97330d00b08332 55c4a0001b9e2d0e00032d78
560c4f458b97330d00b08333 55c4a0001b9e2d0e00032d7b
560c4f458b97330d00b08334 55ce2e78e1055a0d008cad51

我正在尝试编写一个表达式,将该文件转换为一系列SQL语句。所以我试过

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

希望得到一行行

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 = '' 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 = ''

如何纠正我的上述陈述,将令牌放在正确的位置?

3 个答案:

答案 0 :(得分:1)

我在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";
    }

或者作为一个单行:

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

或者使用awk:

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

答案 1 :(得分:0)

一个简短的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

使用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';

答案 2 :(得分:0)

使用Perl的命令行autosplit选项

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

输出

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';