使用grep-bash打印出现文本的行



我想为某些文本生成一个文件,并只输出它出现的行号。示例:

文件文本:

firstsentance
secondsentance
thirdsentance

如果我选择借调,我会得到2的输出。

这将执行您想要的操作:

sed -n '/secondsentence/=' File.text

Perl:

perl -lne 'print $. if /secondsentence/' File.text

我不相信grep本身有这样的功能,但你可以写:

grep -n secondsentance File.text | sed 's/:.*//'

以剥离从CCD_ 2开始的所有内容。

请注意,这将不具有与单独的grep相同的退出代码。


尝试学习AWK:

awk '/secondsentance/ { print NR }' file.text

还有一个awk解决方案。

awk '/secondsentance/ { print NR }' file

最新更新