15 comandos awk interesantes
Este artículo lo guiará a través de algunos comandos awk interesantes y cuándo usarlos. ¡Sigue leyendo para descubrirlo!
Introducción a awk
AWK es un lenguaje popular en UNIX y Linux. Obtuvo su nombre de sus autores: Alfred Aho, Peter Weinberger y Brian Kernighan. El comando awk permite acceder al lenguaje de programación AWK, que está diseñado para procesar datos dentro de flujos de texto.
Popularmente utilizado para escanear patrones y simplificar operaciones complejas, awk lo ayuda a escribir declaraciones efectivas para definir patrones de texto en un archivo. El comando awk luego procesa estas declaraciones leyendo una línea a la vez y toma una acción basada en la condición dada.
En pocas palabras, awk busca y reemplaza texto y ayuda a ordenar, validar o indexar los datos proporcionados.
Características de AWK
awk viene con muchas características únicas:
-
No es necesaria compilación en awk
A menudo se utiliza para la extracción de datos.
Comúnmente utilizado para realizar la manipulación de texto.
Ayuda a generar resultados según sea necesario.
Ahora exploremos el poder de los comandos awk.
15 comandos awk interesantes
Aquí hay una lista compilada de algunos comandos awk interesantes:
- Printing random numbers in a set – Suppose you want to print a few random numbers from a selected pool. You can specify the quantity of random numbers from this pool and ask awk to print this. Here’s an example: let’s print 10 numbers from 0 to 1000. So the awk command for this will be as follows:
awk 'BEGIN { for (i = 1; i <= 10; i++)
print int(1001 * rand()) }'
- Searching for foo or bar – What if you want to write a line in which you want to perform a simple search for foo or bar? Here’s a command that will allow you to do just that:
if (/foo/ || /bar/)
print "Found!"
- Rearranging a field – If you want to print a particular field in a particular order, awk can do it for you. Suppose you want to print the first 5 fields of a particular set in one field per line, you can use the following command:
awk ’{ i = 1
while (i <= 3) {
print $i
i++
}
}’ inputfile
- Splitting a line – In any given set of files, awk can help split a line into fields, where x is the name of the field:
$ awk '{print $x,$x}'xyz.txt
- Running several commands at once – To run several commands at once, you can use a semicolon to specify both the commands:
$ echo "Good morning! Jack" | awk '{$2="Jill"; print $0}'
- Executing an awk script – If you want to execute an awk script from a particular file, you can create a file sum_column and paste the below script in that file:
#!/usr/bin/awk -f
BEGIN {sum=0}
{sum=sum+$x}
END {print sum}In the above script, x equals the column you need to input in the file. On the successful completion of this task, you can use the following command to display the sum of the x column in the input file:
awk -f sum_column input_file.
- Using –f – When coding, it may often seem impractical to refer to the terminal. awk uses –f to perform search from a file:
awk -f script.awk inputfile
- Performing Math functions – You can also use awk for simple Math functions:
awk ’{ sum = $2 + $3 + $4 ; avg = sum / 3
> print $1, avg }’ grades
- Hello World in awk – You can print a simple Hello World in awk using the following command:
awk "BEGIN { print "Hello World!!" }"
You can also create a Hello World program. The following code will not only print the ubiquitous hello message but will also generate header information:
$ awk 'BEGIN { print "Hello World!" }'
- Printing the total number of bytes – You can find out the total bytes used by files using the following command:
ls -l . | awk '{ x += $5 } ; END \
{ print "total bytes: " x }'
total bytes: 7449362
- Anonymizing an Apache log – You can use the following code to anonymize an Apache log:
cat apache-anon-noadmin.log | \
awk 'function ri(n) \
{ return int(n*rand()); } \
BEGIN { srand(); } { if (! \
($5 in jack)) { \
jack[$5] = sprintf("%d.%d.%d.%d", \
ri(255), ri(255)\
, ri(255), ri(255)); } \
$5 = jack[$5]; print __g5_token5b610ba53dbe4 }'
- Operating in rows – If you have an address that you would like to sort in rows, you can do so using the following command:
BEGIN { RS = "" ; FS = "\n" }
{
print "Name is:", $1
print "Address is:", $2
print "City and State are:", $3
print ""
}
- Using the while loop – The while loop keeps on executing the action given to it in a repeated process until the condition is true. For example, for printing numbers from 1 to 100, you can use the following code:
awk 'BEGIN {i = 1; while (i < 100) { print i; ++i } }'
- Using the do-while loop – In this loop, the condition gets executed at the end of the loop even if the statement is false. For example, to print numbers from 1 to 100 using a do-while loop, you can use the following code:
awk 'BEGIN {i = 1; do { print i; ++i } while (i < 100) }'
- Using BEGIN and END – The BEGIN keyword is used to create a header for processing your record:
$ awk 'BEGIN {print "XXX"}
In the same way, the END keyword is used after processing the data:
END {print "File footer"}'