script pasar parametros español ejemplos comando linux bash unix sh

linux - pasar - ¿Cómo limito la cantidad de resultados devueltos desde grep?



shell script ejemplos (4)

Enfoque Awk:

awk ''/pattern/{print; count++; if (count==10) exit}'' file

Me gustaría decir 10 líneas como máximo de grep.

No quiero que mi computadora trabaje duro. Quiero que se detenga después de los 10 resultados encontrados por grep. ¿Es posible?


La opción -m es probablemente lo que estás buscando:

grep -m 10 PATTERN [FILE]

Del man grep ,

-m NUM, --max-count=NUM Stop reading a file after NUM matching lines. If the input is standard input from a regular file, and NUM matching lines are output, grep ensures that the standard input is positioned to just after the last matching line before exiting, regardless of the presence of trailing context lines. This enables a calling process to resume a search.

Nota: grep deja de leer el archivo una vez que se ha encontrado el número especificado de coincidencias.


Otra opción es usar la head :

grep ...parameters... yourfile | head

Esto no requerirá buscar todo el archivo; se detendrá cuando se encuentren las primeras diez líneas coincidentes. Otra ventaja de este enfoque es que devolverá no más de 10 líneas, incluso si está utilizando grep con la opción -o.

Por ejemplo, si el archivo contiene las siguientes líneas:

112233 223344 123123

Entonces esta es la diferencia en el resultado:

$ grep -o ''1.'' yourfile | head -n2 11 12 $ grep -m2 -o ''1.'' 11 12 12

El uso de la head devuelve solo 2 resultados, mientras que -m2 devuelve 3.


Usando la cola:

#dmesg ... ... ... [132059.017752] cfg80211: (57240000 KHz - 65880000 KHz @ 2160000 KHz), (N/A, 4000 mBm) [132116.566238] cfg80211: Calling CRDA to update world regulatory domain [132116.568939] cfg80211: World regulatory domain updated: [132116.568942] cfg80211: (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp) [132116.568944] cfg80211: (2402000 KHz - 2472000 KHz @ 40000 KHz), (300 mBi, 2000 mBm) [132116.568945] cfg80211: (2457000 KHz - 2482000 KHz @ 40000 KHz), (300 mBi, 2000 mBm) [132116.568947] cfg80211: (2474000 KHz - 2494000 KHz @ 20000 KHz), (300 mBi, 2000 mBm) [132116.568948] cfg80211: (5170000 KHz - 5250000 KHz @ 40000 KHz), (300 mBi, 2000 mBm) [132116.568949] cfg80211: (5735000 KHz - 5835000 KHz @ 40000 KHz), (300 mBi, 2000 mBm) [132120.288218] cfg80211: Calling CRDA for country: GB [132120.291143] cfg80211: Regulatory domain changed to country: GB [132120.291146] cfg80211: (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp) [132120.291148] cfg80211: (2402000 KHz - 2482000 KHz @ 40000 KHz), (N/A, 2000 mBm) [132120.291150] cfg80211: (5170000 KHz - 5250000 KHz @ 40000 KHz), (N/A, 2000 mBm) [132120.291152] cfg80211: (5250000 KHz - 5330000 KHz @ 40000 KHz), (N/A, 2000 mBm) [132120.291153] cfg80211: (5490000 KHz - 5710000 KHz @ 40000 KHz), (N/A, 2700 mBm) [132120.291155] cfg80211: (57240000 KHz - 65880000 KHz @ 2160000 KHz), (N/A, 4000 mBm) alex@ubuntu:~/bugs/navencrypt/dev-tools$ dmesg | grep cfg8021 | head 2 head: cannot open ‘2’ for reading: No such file or directory alex@ubuntu:~/bugs/navencrypt/dev-tools$ dmesg | grep cfg8021 | tail -2 [132120.291153] cfg80211: (5490000 KHz - 5710000 KHz @ 40000 KHz), (N/A, 2700 mBm) [132120.291155] cfg80211: (57240000 KHz - 65880000 KHz @ 2160000 KHz), (N/A, 4000 mBm) alex@ubuntu:~/bugs/navencrypt/dev-tools$ dmesg | grep cfg8021 | tail -5 [132120.291148] cfg80211: (2402000 KHz - 2482000 KHz @ 40000 KHz), (N/A, 2000 mBm) [132120.291150] cfg80211: (5170000 KHz - 5250000 KHz @ 40000 KHz), (N/A, 2000 mBm) [132120.291152] cfg80211: (5250000 KHz - 5330000 KHz @ 40000 KHz), (N/A, 2000 mBm) [132120.291153] cfg80211: (5490000 KHz - 5710000 KHz @ 40000 KHz), (N/A, 2700 mBm) [132120.291155] cfg80211: (57240000 KHz - 65880000 KHz @ 2160000 KHz), (N/A, 4000 mBm) alex@ubuntu:~/bugs/navencrypt/dev-tools$ dmesg | grep cfg8021 | tail -6 [132120.291146] cfg80211: (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp) [132120.291148] cfg80211: (2402000 KHz - 2482000 KHz @ 40000 KHz), (N/A, 2000 mBm) [132120.291150] cfg80211: (5170000 KHz - 5250000 KHz @ 40000 KHz), (N/A, 2000 mBm) [132120.291152] cfg80211: (5250000 KHz - 5330000 KHz @ 40000 KHz), (N/A, 2000 mBm) [132120.291153] cfg80211: (5490000 KHz - 5710000 KHz @ 40000 KHz), (N/A, 2700 mBm) [132120.291155] cfg80211: (57240000 KHz - 65880000 KHz @ 2160000 KHz), (N/A, 4000 mBm) alex@ubuntu:~/bugs/navencrypt/dev-tools$