2

如何编写 Perl 脚本将文本文件转换为所有大写字母?

4

4 回答 4

9
perl -ne "print uc" < input.txt

-n您的命令行脚本(由 提供-e)包装在一个while循环中。Auc返回默认变量的 ALL-UPPERCASE 版本,它的作用$_是什么print,你自己知道。;-)

-p就像,-n但它做了一个print加法。同样,作用于默认变量$_

要将其存储在脚本文件中:

#!perl -n
print uc;

像这样称呼它:

perl uc.pl < in.txt > out.txt
于 2011-04-29T12:55:36.727 回答
3
$ perl -pe '$_= uc($_)' input.txt > output.txt
于 2011-04-29T12:47:33.293 回答
2

perl -pe '$_ = uc($_)' input.txt > output.txt

但是,如果您使用的是 Linux(或 *nix),您甚至不需要 Perl。其他一些方法是:

awk:

awk '{ print toupper($0) }' input.txt >output.txt

tr:

tr '[:lower:]' '[:upper:]' < input.txt > output.txt

于 2011-04-29T13:13:25.377 回答
0
$ perl -Tpe " $_ = uc; " --

$ perl -MO=Deparse -Tpe " $_ = uc; " -- a s d f
LINE: while (defined($_ = <ARGV>)) {
    $_ = uc $_;
}
continue {
    die "-p destination: $!\n" unless print $_;
}
-e syntax OK

$ cat myprogram.pl
#!/usr/bin/perl -T --
LINE: while (defined($_ = <ARGV>)) {
    $_ = uc $_;
}
continue {
    die "-p destination: $!\n" unless print $_;
}
于 2011-05-01T04:40:40.770 回答