通过perl在文本文件中插入一行失败

时间:2017-08-17 06:34:49

标签: perl

我正在尝试使用perl在文本文件的第二行插入一个新行。但它一直都在失败。

我使用下面的命令来实现同样的目标。

  perl -ni -e "print; print \"permissibleCars = [ ${part[*]} ]\n\" if $. == 2" query/containerId_count.js

但我一直收到错误: -

 root@vm-test-001:~/mongosearch# distinct_array=`sed ':a;N;$!ba;s/\n/ /g' output/ontainerId_distinct.txt`
 root@vm-test-001:~/mongosearch# declare -a arr=($distinct_array)
 root@vm-test-001:~/mongosearch# batchsize=1
 root@vm-test-001:~/mongosearch# IFS=,
 root@vm-test-001:~/mongosearch# part=( "${arr[@]:i:batchsize}" )
 root@vm-test-001:~/mongosearch# echo $part
 "C:00000092666270:53882159774"
 root@vm-test-001:~/mongosearch# perl -ni -e "print; print \"permissibleCars = [ ${part[*]} ]\n\" if $. == 2" query/containerId_count.js
 Bareword found where operator expected at -e line 1, near ""permissibleCars = [ "C"
    (Missing operator before C?)
String found where operator expected at -e line 1, near "53882159774" ]\n""
    (Missing operator before " ]\n"?)
syntax error at -e line 1, near ""permissibleCars = [ "C"
Illegal octal digit '9' at -e line 1, at end of line
Execution of -e aborted due to compilation errors.

你可以帮我一下吗?

此致

1 个答案:

答案 0 :(得分:1)

试试这个:

export PARTS=${part[*]}
perl -lni -e 'print; print "permissibleCars = [".join(",",split/ /,$ENV{PARTS})."]" if $. == 2' query/containerId_count.js

和 在linux平台上,我们应该使用单引号作为一个班轮。从Perl black book开始,请参阅第19页和第20页。