views:

1607

answers:

6

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

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

+6  A: 

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.)

Stephan202
It is very slow and ineffective approach, see Dave Sherohman's solution http://stackoverflow.com/questions/881931/how-to-print-numbers-of-line-containing-extended-ascii-characters-in-perl/882113#882113 It is far faster and simpler.
Hynek -Pichi- Vychodil
This answer was posted before Dave's. I have seen Dave's approach, and it is to be preferred in most instances. This just shows that I'm a Perl novice. I choose not to delete this answer because the last part appears to do exactly what the questioner wants. Also see http://stackoverflow.com/questions/882122/reading-a-file-char-by-char-and-checking-for-extented-ascii-char
Stephan202
...ah, that page has been deleted. Suffice it to say, the question stated that the line number should be printed for *each* extended ASCII character. This is what my solution does.
Stephan202
+5  A: 

The first printable ASCII character is space (32). The last printable ASCII character is ~ (126). So I'd probably use

while (<>) {
  print "$.\n" if /[^ -~]/;
}

although it will, admittedly, also display lines containing control characters as well as extended ASCII.

Edit: Changed to print the line number rather than the line itself.

Dave Sherohman
It's easy to print the line number instead of the line:while(<>) { print "$.\n" if /[^ -~]/;}This should solve the stated problem
Yaakov Belch
Whoops! I was just reading the question itself and missed that the title specified that he wanted the line number. Thanks for the catch.
Dave Sherohman
+2  A: 

Oneliner:

perl -nE'say$.if/[\xE0-\xFF]/'

for older perl versions

perl -lne'print$.if/[\xE0-\xFF]/'
Hynek -Pichi- Vychodil
+1  A: 

A crucial question is whether the

use bytes;

pragma should be in effect. The poster should decide that. For picking characters with codes greater than 127, the following will suffice:

print grep 127 < ord, split // while <>;

or

print grep /[^[:ascii:]]/, split // while <>;
Sinan Ünür
A: 

Hynek -Pichi- Vychodil's answer:

perl -nE'say$.if/[\xE0-\xFF]/'

only tests a limited part of the non-printing should presumably be

perl -nE'say$.if/[\x80-\xFF]/'

instead.

A: 

What about grep?

grep [\x00-\x1F\x7F-\xFF]+ *
Impress TheNet