简体   繁体   English

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

[英]Splitting lines into individual strings in Perl

Here's my code currently: 这是我目前的代码:

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

So if my test.txt file had the following: 因此,如果我的test.txt文件具有以下内容:

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

My output is: 我的输出是:

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

My desired output is just outputting the word with "test" in it or 我想要的输出只是输出带有“ test”的单词,或者

line 1: testword
line 3: testphrase

I thought by using "split", it changes how Perl reads the input instead of line by line now it would be word by word but it doesn't seem to work. 我认为通过使用“ split”,它会更改Perl读取输入的方式,而不是逐行地读取输入内容,这现在是逐字逐句的,但似乎不起作用。 Any thoughts or suggestions? 有什么想法或建议吗?

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