繁体   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