在Perl中将行拆分为单个字符串

时间:2017-05-31 21:27:48

标签: 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读取输入而不是逐行读取的方式,现在它将逐字逐句但它似乎不起作用。有什么想法或建议吗?

1 个答案:

答案 0 :(得分:0)

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