¿Cómo puedo formatear mi salida grep para mostrar los números de línea al final de la línea, y también el número de visitas?


Estoy usando grep para hacer coincidir la cadena en un archivo. Aquí hay un archivo de ejemplo:

example one,
example two null,
example three,
example four null,

grep -i null myfile.txt retornos

example two null,
example four null,

¿Cómo puedo devolver las líneas coincidentes junto con sus números de línea de esta manera:

  example two null, - Line number : 2
  example four null, - Line number : 4
  Total null count : 2

Sé que-c devuelve el total de líneas coincidentes, pero no sé cómo formatearlo correctamente para agregar total null count al frente, y no sé cómo agregar los números de línea.

¿Qué puedo hacer?

Author: Paŭlo Ebermann, 2010-10-19

7 answers

-n devuelve el número de línea.

-i es para ignorar caso. Solo debe utilizarse si no es necesaria la coincidencia de mayúsculas y minúsculas

$ grep -in null myfile.txt

2:example two null,
4:example four null,

Combine con awk para imprimir el número de línea después del partido:

$ grep -in null myfile.txt | awk -F: '{print $2" - Line number : "$1}'

example two null, - Line number : 2
example four null, - Line number : 4

Utilice la sustitución de comandos para imprimir el recuento total de nulos:

$ echo "Total null count :" $(grep -ic null myfile.txt)

Total null count : 2
 482
Author: dogbane,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-08 18:20:38

Utilice -n o --line-number.

Echa un vistazo a man grep para muchas más opciones.

 46
Author: Andy Lester,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-10-19 12:10:06

O use awk en su lugar:

awk '/null/ { counter++; printf("%s%s%i\n",$0, " - Line number: ", NR)} END {print "Total null count: " counter}' file
 6
Author: Zsolt Botykai,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-10-20 08:23:50

Use grep -n -i null myfile.txt para mostrar el número de línea delante de cada partido.

No creo que grep tenga un interruptor para imprimir el recuento de líneas totales coincidentes, pero solo puede canalizar la salida de grep en wc para lograr eso:

grep -n -i null myfile.txt | wc -l
 5
Author: WakiMiko,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-10-19 12:13:11

grep encuentra las líneas y muestra los números de línea, pero no te permite "programar" otras cosas. Si desea incluir texto arbitrario y hacer otra "programación", puede utilizar awk,

$ awk '/null/{c++;print $0," - Line number: "NR}END{print "Total null count: "c}' file
example two null,  - Line number: 2
example four null,  - Line number: 4
Total null count: 2

O solo usando el shell (bash/ksh)

c=0
while read -r line
do
  case "$line" in
   *null* )  (
    ((c++))
    echo "$line - Line number $c"
    ;;
  esac
done < "file"
echo "total count: $c"
 3
Author: ghostdog74,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-10-19 12:13:44

O en perl (para completar...):

perl -npe 'chomp; /null/ and print "$_ - Line number : $.\n" and $i++;$_="";END{print "Total null count : $i\n"}'
 2
Author: hannes,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-10-19 12:28:18

Consulte este enlace para linux comando linux http://linuxcommand.org/man_pages/grep1.html

Para mostrar la línea no ,la línea de código y el archivo use este comando en su terminal o cmd, GitBash(alimentado por terminal)

grep -irn "YourStringToBeSearch"
 1
Author: Vrushal Raut,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-06 15:54:32