Hi i am inserting image location into particular line of file using Perl one-liner code as below
您好我正在使用Perl单行代码将图像位置插入特定的文件行,如下所示
my $image="/home/users/images/image1.tar";
system(q(perl -pi -e 'print "\n$image" if ($. == 5 && $_=~ /^\s*$/ )' myfile.txt));
i am not able to insert a image location into the file . please can anyone help me out?
我无法在图像中插入图像位置。请有人帮帮我吗?
2 个解决方案
#1
1
As was pointed out, using single quotes q{}
did not allow your $image
variable to interpolate.
正如所指出的,使用单引号q {}不允许$ image变量进行插值。
To fix, just concatenate that variable into your string:
要修复,只需将该变量连接到您的字符串:
#!/usr/bin/perl
use strict;
use warnings;
my $image = '/home/users/images/image1.tar';
system(q{perl -pi -e 'print "\n} . $image . q{" if ($. == 5 && $_=~ /^\s*$/ )' myfile.txt});
However, a much better solution is just to do this processing local to your perl script.
但是,更好的解决方案就是对perl脚本进行本地处理。
The following does the exact same processing without the secondary call to perl by using $INPLACE_EDIT
:
以下通过使用$ INPLACE_EDIT进行完全相同的处理而没有对perl进行二次调用:
my $image = "/home/users/images/image1.tar";
local @ARGV = 'myfile.txt';
local $^I = '';
while (<>) {
print "\n$image" if $. == 5 && $_ =~ /^\s*$/;
print;
}
For additional methods for editing a file, just read perlfaq5 - How do I change, delete, or insert a line in a file, or append to the beginning of a file?
有关编辑文件的其他方法,请阅读perlfaq5 - 如何更改,删除或插入文件中的行,或附加到文件的开头?
#2
1
I'm not sure why you're shelling out to a second Perl process to do this. Why not just do the processing within the original Perl program?
我不确定你为什么要冒第二个Perl进程来执行此操作。为什么不在原始Perl程序中进行处理?
But your problem seems to be that the string you're passing to system is in single quotes (using q(...)
) which means that the $image
variable won't be expanded. You probably want to change that to a double-quoted string (using qq(...)
).
但是你的问题似乎是你传递给系统的字符串是单引号(使用q(...)),这意味着$ image变量不会被扩展。您可能希望将其更改为双引号字符串(使用qq(...))。
Update:
更新:
This is why shelling out to an external process is fraught with difficulty. You have one variable ($image
) which needs to be passed though and another variable ($_
) which needs to be internal to the second process. You also have an escape sequence (\s
) which the shell is trying (but failing) to interpret.
这就是为什么炮轰外部过程充满困难的原因。你有一个需要传递的变量($ image)和另一个需要在第二个进程内部的变量($ _)。你还有一个转义序列(\ s),shell正在尝试(但没有)解释。
Liberal application of backslashes to escape special characters gives this:
*应用反斜杠来转义特殊字符可以得到:
#!/usr/bin/perl
use strict;
use warnings;
my $image='/home/users/images/image1.tar';
system(qq(perl -pi -e 'print "\n$image" if (\$. == 5 && /^\\s*\$/ )' myfile.txt));
Which seems to work. But I still think you'd be far better off doing this all in one Perl program.
这似乎工作。但我仍然认为你在一个Perl程序中完成所有这些工作要好得多。
#1
1
As was pointed out, using single quotes q{}
did not allow your $image
variable to interpolate.
正如所指出的,使用单引号q {}不允许$ image变量进行插值。
To fix, just concatenate that variable into your string:
要修复,只需将该变量连接到您的字符串:
#!/usr/bin/perl
use strict;
use warnings;
my $image = '/home/users/images/image1.tar';
system(q{perl -pi -e 'print "\n} . $image . q{" if ($. == 5 && $_=~ /^\s*$/ )' myfile.txt});
However, a much better solution is just to do this processing local to your perl script.
但是,更好的解决方案就是对perl脚本进行本地处理。
The following does the exact same processing without the secondary call to perl by using $INPLACE_EDIT
:
以下通过使用$ INPLACE_EDIT进行完全相同的处理而没有对perl进行二次调用:
my $image = "/home/users/images/image1.tar";
local @ARGV = 'myfile.txt';
local $^I = '';
while (<>) {
print "\n$image" if $. == 5 && $_ =~ /^\s*$/;
print;
}
For additional methods for editing a file, just read perlfaq5 - How do I change, delete, or insert a line in a file, or append to the beginning of a file?
有关编辑文件的其他方法,请阅读perlfaq5 - 如何更改,删除或插入文件中的行,或附加到文件的开头?
#2
1
I'm not sure why you're shelling out to a second Perl process to do this. Why not just do the processing within the original Perl program?
我不确定你为什么要冒第二个Perl进程来执行此操作。为什么不在原始Perl程序中进行处理?
But your problem seems to be that the string you're passing to system is in single quotes (using q(...)
) which means that the $image
variable won't be expanded. You probably want to change that to a double-quoted string (using qq(...)
).
但是你的问题似乎是你传递给系统的字符串是单引号(使用q(...)),这意味着$ image变量不会被扩展。您可能希望将其更改为双引号字符串(使用qq(...))。
Update:
更新:
This is why shelling out to an external process is fraught with difficulty. You have one variable ($image
) which needs to be passed though and another variable ($_
) which needs to be internal to the second process. You also have an escape sequence (\s
) which the shell is trying (but failing) to interpret.
这就是为什么炮轰外部过程充满困难的原因。你有一个需要传递的变量($ image)和另一个需要在第二个进程内部的变量($ _)。你还有一个转义序列(\ s),shell正在尝试(但没有)解释。
Liberal application of backslashes to escape special characters gives this:
*应用反斜杠来转义特殊字符可以得到:
#!/usr/bin/perl
use strict;
use warnings;
my $image='/home/users/images/image1.tar';
system(qq(perl -pi -e 'print "\n$image" if (\$. == 5 && /^\\s*\$/ )' myfile.txt));
Which seems to work. But I still think you'd be far better off doing this all in one Perl program.
这似乎工作。但我仍然认为你在一个Perl程序中完成所有这些工作要好得多。