如何搜索文本然后打印结果?

时间:2022-11-29 21:37:14

I am searching for "o" then prints all lines with "o". Any suggestion/code I must apply?

我正在搜索“o”然后用“o”打印所有行。我必须申请的任何建议/代码?

data.txt:

j,o,b:
a,b,d:
o,l,e:
f,a,r:
e,x,o:

desired output:

j,o,b:
o,l,e:
e,x,o:

6 个解决方案

#1


9  

grep o data.txt

perl -ne 'print if (/o/);' <data.txt

#2


4  

If you have grep on your system, then grep o data.txt from the command line should do the trick.

如果你的系统上有grep,那么命令行中的grep o data.txt应该可以解决问题。

Failing that, you could try Perl:

如果做不到这一点,你可以尝试Perl:

open IN, 'data.txt';
my @l = <IN>;
close IN;
foreach my $l (@l) {
   $l =~ /o/ and print $l;
}

#3


1  

grep "o" data.txt

Does that help? I don't know Perl, but you can get the same output using the above grep.

这有帮助吗?我不知道Perl,但你可以使用上面的grep获得相同的输出。

#4


1  

print if /o/;

#5


0  

In Perl:

while (<>) { print if /o/; }

or with grep:

或者用grep:

grep 'o' data.txt

#6


0  

as a very short one-liner:

作为一个非常短的单行:

> perl -pe'$_ x=/o/' filename

#1


9  

grep o data.txt

perl -ne 'print if (/o/);' <data.txt

#2


4  

If you have grep on your system, then grep o data.txt from the command line should do the trick.

如果你的系统上有grep,那么命令行中的grep o data.txt应该可以解决问题。

Failing that, you could try Perl:

如果做不到这一点,你可以尝试Perl:

open IN, 'data.txt';
my @l = <IN>;
close IN;
foreach my $l (@l) {
   $l =~ /o/ and print $l;
}

#3


1  

grep "o" data.txt

Does that help? I don't know Perl, but you can get the same output using the above grep.

这有帮助吗?我不知道Perl,但你可以使用上面的grep获得相同的输出。

#4


1  

print if /o/;

#5


0  

In Perl:

while (<>) { print if /o/; }

or with grep:

或者用grep:

grep 'o' data.txt

#6


0  

as a very short one-liner:

作为一个非常短的单行:

> perl -pe'$_ x=/o/' filename