2011-04-29 108 views

回答

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

-nwhile循环包裹您的命令行脚本(其由-e提供)。 A uc返回默认变量$_的ALL-UPPERCASE版本,以及print的功能,您自己也了解它。 ;-)

-p就像-n,但它另外还有一个print。再次,作用于默认变量$_

要存储在一个脚本文件:

#!perl -n 
print uc; 

这样称呼它:

perl uc.pl <in.txt> out.txt 
+0

为'utf-8'文件添加'-C',例如'echoaßbc| perl -C -ne'print uc'' - >'ASSBC'。 – jfs 2011-04-29 13:17:42

+0

谢谢,迈克尔!这对我有效。信息很有用,我修改它输出到一个文件,这很简单。对于其他新手,如果不在当前目录中,则必须在目录和文件名周围放置双引号。 – salvationishere 2011-04-29 13:31:57

+0

还有一个后续问题......我如何写这个并将其存储在Perl文件中?所以我只是给它输入文件和输出文件参数? – salvationishere 2011-04-29 13:34:42

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

感谢马修。我试过这个,但我用目录信息稍微修改了一下。但是,这给了我错误:“无法找到字符串终止符”“”在EOF之前的任何位置在-e行1“ – salvationishere 2011-04-29 13:25:52

2

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

但你不要,即使你使用的是Linux(需要的Perl或* nix)。其他一些方法是:

AWK:

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

TR:

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

+0

感谢您的信息。虽然我不使用Linux。 – salvationishere 2011-04-29 13:32:26

+0

好的,np。尽管Perl对Linux用户有很大的帮助,但我一直认为这对Windows用户来说更有帮助;仅仅因为Linux拥有丰富的工具,可以通过许多标准实用程序完成一项任务。 – 2011-04-29 14:18:57

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 $_; 
}