查找文件中的一个字的行号 [英] find line number of a word in a file
问题描述
我有一个文件:file.txt的,其中包含了以下数据。 GNU的grep版本:2.5.1,系统管理员说,没有将其升级到更高版本,因为它可能会影响生产。
这是一个文件,我的名字是卡尔,这是什么过程,卡尔卡尔初中,文件是一个测试文件,文件的名字是file.txt的
我的名字是不是卡尔,我的名字是卡尔·乔伊
你叫什么名字?
你知不知道你的名字和它是什么?
当我运行下面的命令得到一个字的行#是使用下面的命令这个文件,它给了我喜欢的输出:
$ grep的 - 现在是file.txt的
1:是
是
是
是
是
是
2:是
是
3:是
4:是
我可以运行什么命令得到以下的输出:
1:是
1:是
1:是
1:是
1:是
1:是
2:是
2:是
3:是
4:是
或
为:1,1,1,1,1
是:2,2
是:3
是:4
如果我想下面的命令,我越来越接近,但我想是更换1是1(以逗号分隔)
的grep - 现在是file.txt的| TR'\\ 012'''| SEDS /([0-9]:)/ \\ n \\ 1 / G| grep的。
1:是是是是是
2:是的
3:是
4:是
如果你可以使用 perl的
然后在这里用他们的最后一场比赛的开始(@ - )和的最后一场比赛结束(@ +):
perl的-lne
而($ _ =〜/ \\二\\ B / G){
打印$:SUBSTR($ _,$ - [0],$ + [0] - $ - [0]);
}'文件
1:是
1:是
1:是
1:是
1:是
1:是
2:是
2:是
3:是
4:是
根据OP的要求新格式:
perl的-lne
$发现=()= / \\二\\ b / g标准;
打印SUBSTR($ _,$ - [0],$ + [0] - $ - [0]),:,加入(,($)X $找到);
'文件
是:1,1,1,1,1,1
是:2,2
是:3
是:4
使用GNU AWK
word边界:
徒劳无功{
N = GSUB(/ \\<是\\> /,);
printf的%S:,是;
对于(i = 1; I< = N;我++)的printf%s%S,NR,(我==ñRS:?,)
}'文件
是:1,1,1,1,1,1
是:2,2
是:3
是:4
使用香草 AWK
(礼貌<一个href=\"http://stackoverflow.com/questions/25336239/find-line-number-of-a-word-in-a-file/25336554?noredirect=1#comment39525738_25336554\">Ed莫顿(见注释)):
的awk'
{
N = GSUB(/(^ | [^ [:字母:])是([^ [:字母:] | $)/,);
printf的%S:,是;
对于(i = 1; I&LT; = N;我++)的printf%s%S,NR,(我==ñRS:?,)
}'文件
是:1,1,1,1,1,1
是:2,2
是:3
是:4
I have a file: file.txt, which contains the following data in it. GNU grep version: 2.5.1, System admin says NO to upgrading it to later version as it might impact production
This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt
My name is not Karl, my name is Karl Joey
What is your name?
Do you know your name and what it is?
When I'm running the following command to get line# of a word "is" in this file using the following command, it's giving me the output like:
$ grep -now "is" file.txt
1:is
is
is
is
is
is
2:is
is
3:is
4:is
What command can I run to get the following output:
1:is
1:is
1:is
1:is
1:is
1:is
2:is
2:is
3:is
4:is
OR
is:1,1,1,1,1
is:2,2
is:3
is:4
If I'm trying the following command, I'm getting close but I want to replace 1 with is and is with 1 (comma separated)
grep -now "is" file.txt | tr '\012' ' '| sed "s/([0-9]:)/\n\1/g" | grep "."
1:is is is is is is
2:is is
3:is
4:is
If you can use perl
then here is using their last match start (@-) and last match end (@+):
perl -lne '
while ($_ =~ /\bis\b/g) {
print "$.:", substr($_, $-[0], $+[0] - $-[0]);
}' file
1:is
1:is
1:is
1:is
1:is
1:is
2:is
2:is
3:is
4:is
New format based on OP's request:
perl -lne '
$found =()= /\bis\b/g;
print substr($_, $-[0], $+[0] - $-[0]), ":", join (",", ($.) x $found);
' file
is:1,1,1,1,1,1
is:2,2
is:3
is:4
Using GNU awk
for word boundaries:
gawk '{
n = gsub(/\<is\>/,"");
printf "%s:", "is";
for (i=1; i<=n; i++) printf "%s%s", NR, (i==n?RS:",")
}' file
is:1,1,1,1,1,1
is:2,2
is:3
is:4
Using vanilla awk
(Courtesy Ed Morton (see comments)):
awk '
{
n = gsub(/(^|[^[:alpha:]])is([^[:alpha:]]|$)/,"");
printf "%s:", "is";
for (i=1; i<=n; i++) printf "%s%s", NR, (i==n?RS:",")
}' file
is:1,1,1,1,1,1
is:2,2
is:3
is:4
这篇关于查找文件中的一个字的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!