簡體   English   中英

在Perl中將行拆分為單個字符串

[英]Splitting lines into individual strings in Perl

這是我目前的代碼:

    open (MYFILE, "text.txt"); 
    while(<MYFILE>){
        split; 
        if (m,test,){
            print $_; 
        }
    }
    close(MYFILE); 

因此,如果我的test.txt文件具有以下內容:

line 1: this is a test line
line 2: this has nothing
line 3: oh here's a test

我的輸出是:

line 1: this is a testword line
line 3: oh here's a testphrase

我想要的輸出只是輸出帶有“ test”的單詞,或者

line 1: testword
line 3: testphrase

我認為通過使用“ split”,它會更改Perl讀取輸入的方式,而不是逐行地讀取輸入內容,這現在是逐字逐句的,但似乎不起作用。 有什么想法或建議嗎?

open (MYFILE, "text.txt"); 
while(<MYFILE>) {
    print grep /test/, split; 
}
close(MYFILE); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM