Scripting util curl awk bash y parseos
Este post reune patrones utiles para automatizar tareas, probar endpoints HTTP, parsear logs y construir pequenos scripts shell.
curl
1
2
3
4
5
| curl -Ik https://app.example.local/
curl -sS https://app.example.local/api/health
curl -X POST https://app.example.local/api \
-H 'Content-Type: application/json' \
-d '{"clave":"valor"}'
|
awk
1
2
3
| awk '{print $1}' fichero.log
awk -F';' '{print $1,$3}' fichero.csv
awk '$9 ~ /^5/ {print $0}' access.log
|
regex utiles
1
2
| grep -E 'ERROR|WARN|FATAL' app.log
grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' app.log | sort -u
|
parsear logs HTTP
1
2
| awk '{print $1}' access.log | sort | uniq -c | sort -nr | head
awk '{print $9}' access.log | sort | uniq -c | sort -nr
|
Timestamps en scripts
1
2
3
| fecha="$(date +%Y%m%d_%H%M%S)"
log="/var/log/script_${fecha}.log"
printf '[%s] inicio\n' "$(date --iso-8601=seconds)" >> "$log"
|
Comparar listas de ficheros
1
2
| sort origen.txt > origen.sorted
sort existentes.txt > existentes.sorted
|
Ficheros presentes en origen y no en existentes:
1
| comm -23 origen.sorted existentes.sorted
|
Ficheros presentes en ambas listas:
1
| comm -12 origen.sorted existentes.sorted
|
Leer fichero y ejecutar accion
1
2
3
| while IFS= read -r linea; do
printf 'Procesando %s\n' "$linea"
done < entrada.txt
|
Copiar a varios hosts
1
2
3
| while IFS= read -r host; do
scp -r /origen/ "<USUARIO>@${host}:/destino/"
done < hosts.txt
|
Generar datos ficticios para pruebas
1
| python3 herramienta.py "<NOMBRE_FICTICIO>"
|
plantilla bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| #!/usr/bin/env bash
set -euo pipefail
main() {
local fichero="${1:-}"
if [[ -z "$fichero" ]]; then
printf 'Uso: %s <fichero>\n' "$0" >&2
exit 1
fi
grep -E 'ERROR|WARN' "$fichero"
}
main "$@"
|