如何使用Perl在文件中找到扩展ASCII字符? [英] How can I find extended ASCII characters in a file using Perl?

查看:138
本文介绍了如何使用Perl在文件中找到扩展ASCII字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Perl在文件中找到扩展ASCII字符?任何人都可以得到脚本?



.....提前感谢.....

解决方案

由于扩展的 ASCII 字符的值为 128及更高版本,您可以调用 ord ,并处理值> = 128的值。以下代码从stdin读取,仅打印扩展的ASCII字符:


$ ($($)$($($)$($) )= 128);
}
}

或者, unpack 连同 chr 也可以工作。例如:

  while(<>){
foreach(unpack(C *,$ _) ){
print(chr($ _))if($ _> = 128);
}
}

(我相信一些Perl大师可以凝聚两者这些到两个单行...)






要打印行号,您可以使用以下(这不会删除重复,并且在unicode被传递时会有奇怪的行为):

  while(<>){
while(/(.)/g){
print($。。\\\
)if(ord($ 1)> = 128);
}
}

(感谢 Yaakov Belch $。提示。)


How can I find extended ASCII characters in a file using Perl? Can anyone get the script?

.....thanks in advance.....

解决方案

Since the extended ASCII characters have value 128 and higher, you can just call ord on individual characters and handle those with a value >= 128. The following code reads from stdin and prints only the extended ASCII characters:

while (<>) {
  while (/(.)/g) {
    print($1) if (ord($1) >= 128);
  }
}

Alternatively, unpack together with chr will also work. Example:

while (<>) {
  foreach (unpack("C*", $_)) {
    print(chr($_)) if ($_ >= 128);
  }
}

(I'm sure some Perl guru can condense both of these to two one-liners...)


To print the line numbers instead, you can use the following (this does not remove duplicates, and will have odd behaviour when unicode is passed):

while (<>) {
  while (/(.)/g) {
    print($. . "\n") if (ord($1) >= 128);
  }
}

(Thanks Yaakov Belch for the $. tip.)

这篇关于如何使用Perl在文件中找到扩展ASCII字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆