2011-04-29 106 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 $_; 
}