简体   繁体   English

如何强制 perl 仅从 stdin 而不是从命令行上的文件处理 args?

[英]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.

相关问题 如何让Perl脚本接受来自STDIN和命令行参数的参数? - How can I get a Perl script to accept parameters from both STDIN and command line arguments? 如何从Perl脚本中捕获系统命令的stdin和stdout? - How can I capture the stdin and stdout of system command from a Perl script? 如何在命令行中从sed更改为Perl语句? - How can I change from a sed to Perl statement in command line? 如何在Perl中从命令行读取两个文件? - How can I read two files from command line in Perl? 如何使用 Perl 从 Windows 命令行获取文件的 SHA1 哈希? - How can I use Perl to get a SHA1 hash of a file from the Windows command line? 我可以从标准输入运行 Perl 脚本吗? - Can I run a Perl script from stdin? Perl:使用Getopt :: Long从命令行和[piped STDIN | file]中获取参数 - Perl: Take arguments from both command line and [piped STDIN|file], using Getopt::Long 传递命令行参数以及来自STDIN的Perl脚本输入? - Pass command line arguments as well as input from STDIN for Perl script? 如何不使用`while(&lt;&gt;)`来读取STDIN和作为命令行参数传递的文件? - How can I read from both STDIN and files passed as command line arguments without using `while(<>)`? Perl-从命令行检测文件是否只有指定字符 - Perl - Detect from command line if file has only a specified character
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM