[英]How can I force perl to process args ONLY from stdin and not from a file on command line?
If I have this inline command:如果我有这个内联命令:
perl -pi -e 's/([\da-f]{2})([\da-f]{2})\s?/\\x$1\\x$2\t/g'
Which is simply to substitute four-digit hex, and add it a 'x' in front.这只是替换四位十六进制,并在前面添加一个“x”。
-i
used with no filenames on the command line, reading from STDIN
. -i
在命令行上不带文件名使用,从STDIN
读取。 So for params: 0000 0776
, results are \\x00\\x00\\x07\\x76
所以对于参数:
0000 0776
,结果是\\x00\\x00\\x07\\x76
I know, that if -n
or -p
(with printing) called, perl takes <>
diamond.我知道,如果
-n
或-p
(带打印)被调用,perl 需要<>
菱形。 But I want to pass args only AFTER command, but perl assumes it as files to read.但我只想在命令之后传递 args,但 perl 将其假定为要读取的文件。 So how do I force
-n
or -p
to regard args after command to be regular args for <>
in program, and not args as files to read?那么如何强制
-n
或-p
将命令后的 args 视为程序中<>
常规 args,而不是 args 作为要读取的文件?
Also, I do not understand the role of i
here.另外,我不明白
i
在这里的作用。 If i would not include it, then I would be adding args line after line (as does <>
), but with i
, it takes all my args at once?如果我不包含它,那么我会一行一行地添加 args(就像
<>
),但是对于i
,它会一次占用我所有的 args 吗?
If there are no arguments (ie, if @ARGV
is empty), then your one-line script (which implicitly uses <>
) will read input from STDIN
.如果没有参数(即,如果
@ARGV
为空),那么您的单行脚本(隐式使用<>
)将从STDIN
读取输入。 So the solution is to clear @ARGV
at compile time.所以解决方案是在编译时清除
@ARGV
。
perl -pi -e 'BEGIN{@ARGV=()}
s/([\da-f]{2})([\da-f]{2})\s?/\\x$1\\x$2\t/g'
Another solution: Force ARGV
(the implicit file handle that the base <>
operator reads from) to point to STDIN
.另一种解决方案:强制
ARGV
(基本<>
运算符读取的隐式文件句柄)指向STDIN
。 This solution doesn't clobber your @ARGV
, if any.此解决方案不会破坏您的
@ARGV
,如果有的话。
perl -pi -e 'BEGIN{*ARGV=*STDIN}
s/([\da-f]{2})([\da-f]{2})\s?/\\x$1\\x$2\t/g'
The -p option is equivalent to the following code : -p 选项等效于以下代码:
LINE:
while (<>) {
... # your program goes here
} continue {
print or die "-p destination: $!\n";
}
-n is the same without the continue block. -n 是相同的,没有 continue 块。 There's no way to change what it reads from (which is unfortunate, since
<<>>
and <STDIN>
are both safer options), but it's pretty easy to replicate it with your modification (the error checking is rarely necessary here):无法更改它读取的内容(不幸的是,因为
<<>>
和<STDIN>
都是更安全的选项),但是通过修改复制它很容易(此处很少需要错误检查):
perl -e 'while (<STDIN>) { s/([\da-f]{2})([\da-f]{2})\s?/\\x$1\\x$2\t/g } continue { print }'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.