find line number of a word in a file

2019-09-14 23:15发布

问题:

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

回答1:

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


回答2:

grep -o -n "is" file.txt

seems to work on my system.



回答3:

If you can upgrade your version of grep, it should fix it:

# grep --version
grep (GNU grep) 2.16
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.
# grep -now is file
1:is
1:is
1:is
1:is
1:is
1:is
2:is
2:is
3:is
4:is

Imitating grep's output using perl:

# perl -lne '$x = "is"; $c = () = /\b$x\b/g; while ($c--) { print "$.:$x"; }' file
1:is
1:is
1:is
1:is
1:is
1:is
2:is
2:is
3:is
4:is

Another:

# perl -lne '$x = "is"; $c = () = /\b$x\b/g; next unless $c--; $t = "${x}:$."; $t .= ",$." while ($c--); print $t' file
is:1,1,1,1,1,1
is:2,1
is:3
is:4