¿Cómo conseguir que "wc-l" imprima solo el número de líneas sin nombre de archivo?


wc -l file.txt

Muestra el número de líneas y el nombre del archivo.

Solo necesito el número en sí (no el nombre del archivo).

Puedo hacer esto

 wc -l file.txt | awk '{print $1}'

Pero tal vez hay una mejor manera?

 90
Author: PoGibas, 2012-04-20

7 answers

Intente de esta manera:

wc -l < file.txt
 145
Author: Norman Ramsey,
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
2015-05-28 06:08:15
cat file.txt | wc -l

De acuerdo con la página de manual (para la versión BSD, no tengo una versión GNU para comprobar):

Si no se especifican archivos, se utiliza la entrada estándar y no hay archivo nombre Visualiza. El prompt aceptará la entrada hasta recibir EOF, o [^D] en la mayoría de los entornos.

 18
Author: pjmorse,
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
2012-04-19 23:41:02

¿Qué tal

wc -l file.txt | cut -d' ' -f1

Es decir, canalizar la salida de wc en cut (donde los delimitadores son espacios y seleccionar solo el primer campo)

 10
Author: Neil Albert,
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
2013-08-06 00:13:12

Para hacer esto sin el espacio principal, por qué no:

wc -l < file.txt | bc
 8
Author: Desi Cochrane,
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-09-17 08:38:59

¿Qué tal

grep -ch "^" file.txt
 4
Author: MeIsMich,
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
2014-10-20 05:22:20

Obviamente, hay muchas soluciones a esto. Aquí hay otro sin embargo:

wc -l somefile | tr -d "[:alpha:][:blank:][:punct:]"

Esto solo muestra el número de líneas, pero el carácter de nueva línea final (\n) está presente, si tampoco lo desea, reemplace [:blank:] por [:space:].

 2
Author: Bouchaala Reda,
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-01-25 15:54:58

La mejor manera sería primero buscar todos los archivos en el directorio y luego usar AWK NR (Número de Registros Variable)

A continuación se muestra el comando :

find <directory path>  -type f | awk  'END{print NR}'

Ejemplo : - find /tmp/ -type f | awk 'END{print NR}'

 0
Author: user128364,
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-11-02 11:01:59