mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-04 13:57:38 +02:00
string patterns
This commit is contained in:
@@ -197,14 +197,14 @@ function string_functions( localvar, arr) {
|
|||||||
sub("fo+", "Meet me at the ", localvar) # localvar => "Meet me at the bar"
|
sub("fo+", "Meet me at the ", localvar) # localvar => "Meet me at the bar"
|
||||||
gsub("e+", ".", localvar) # localvar => "m..t m. at th. bar"
|
gsub("e+", ".", localvar) # localvar => "m..t m. at th. bar"
|
||||||
|
|
||||||
# Search for a string that matches a regular expression
|
# Buscar una cadena que haga match con una expresión regular
|
||||||
# index() does the same thing, but doesn't allow a regular expression
|
# index() hace lo mismo, pero no permite expresiones regulares
|
||||||
match(localvar, "t") # => 4, since the 't' is the fourth character
|
match(localvar, "t") # => 4, dado que 't' es el cuarto caracter
|
||||||
|
|
||||||
# Split on a delimiter
|
# Separar con base en un delimitador
|
||||||
split("foo-bar-baz", arr, "-") # a => ["foo", "bar", "baz"]
|
split("foo-bar-baz", arr, "-") # a => ["foo", "bar", "baz"]
|
||||||
|
|
||||||
# Other useful stuff
|
# Otras funciones útiles
|
||||||
sprintf("%s %d %d %d", "Testing", 1, 2, 3) # => "Testing 1 2 3"
|
sprintf("%s %d %d %d", "Testing", 1, 2, 3) # => "Testing 1 2 3"
|
||||||
substr("foobar", 2, 3) # => "oob"
|
substr("foobar", 2, 3) # => "oob"
|
||||||
substr("foobar", 4) # => "bar"
|
substr("foobar", 4) # => "bar"
|
||||||
@@ -215,87 +215,86 @@ function string_functions( localvar, arr) {
|
|||||||
|
|
||||||
function io_functions( localvar) {
|
function io_functions( localvar) {
|
||||||
|
|
||||||
# You've already seen print
|
# Ya has visto print
|
||||||
print "Hello world"
|
print "Hello world"
|
||||||
|
|
||||||
# There's also printf
|
# También hay printf
|
||||||
printf("%s %d %d %d\n", "Testing", 1, 2, 3)
|
printf("%s %d %d %d\n", "Testing", 1, 2, 3)
|
||||||
|
|
||||||
# AWK doesn't have file handles, per se. It will automatically open a file
|
# AWK no tiene handles de archivos en sí mismo. Automáticamente abrirá un
|
||||||
# handle for you when you use something that needs one. The string you used
|
# handle de archivo cuando use algo que necesite uno. El string que usaste
|
||||||
# for this can be treated as a file handle, for purposes of I/O. This makes
|
# para esto puede ser tratada como un handle de archivo para propósitos de I/O.
|
||||||
# it feel sort of like shell scripting:
|
# Esto lo hace similar al scripting de shell:
|
||||||
|
|
||||||
print "foobar" >"/tmp/foobar.txt"
|
print "foobar" >"/tmp/foobar.txt"
|
||||||
|
|
||||||
# Now the string "/tmp/foobar.txt" is a file handle. You can close it:
|
# Ahora el string "/tmp/foobar.txt" es un handle. Puedes cerrarlo:
|
||||||
close("/tmp/foobar.txt")
|
close("/tmp/foobar.txt")
|
||||||
|
|
||||||
# Here's how you run something in the shell
|
# Aquí está como correr algo en el shell
|
||||||
system("echo foobar") # => prints foobar
|
system("echo foobar") # => muestra foobar
|
||||||
|
|
||||||
# Reads a line from standard input and stores in localvar
|
# Lee una línea de la entrada estándar (stdin) y lo guarda en localvar
|
||||||
getline localvar
|
getline localvar
|
||||||
|
|
||||||
# Reads a line from a pipe
|
# Lee una línea desde un pipe
|
||||||
"echo foobar" | getline localvar # localvar => "foobar"
|
"echo foobar" | getline localvar # localvar => "foobar"
|
||||||
close("echo foobar")
|
close("echo foobar")
|
||||||
|
|
||||||
# Reads a line from a file and stores in localvar
|
# Lee una línea desde un archivo y la guarda en localvar
|
||||||
getline localvar <"/tmp/foobar.txt"
|
getline localvar <"/tmp/foobar.txt"
|
||||||
close("/tmp/foobar.txt")
|
close("/tmp/foobar.txt")
|
||||||
}
|
}
|
||||||
|
|
||||||
# As I said at the beginning, AWK programs consist of a collection of patterns
|
# Como dije al inicio, los programas en AWK son una colección de patrones y
|
||||||
# and actions. You've already seen the all-important BEGIN pattern. Other
|
# acciones. Ya conociste el patrón BEGIN. otros patrones sólo se usan si estás
|
||||||
# patterns are used only if you're processing lines from files or standard
|
# procesando líneas desde archivos o stdin.
|
||||||
# input.
|
|
||||||
#
|
# Cuando pasas argumentos a AWK, son tratados como nombres de archivos a
|
||||||
# When you pass arguments to AWK, they are treated as file names to process.
|
# procesar. Los va a procesar todos, en orden. Imagínalos como un ciclo for
|
||||||
# It will process them all, in order. Think of it like an implicit for loop,
|
# implícito, iterando sobre las líneas de estos archivos. Estos patrones y
|
||||||
# iterating over the lines in these files. these patterns and actions are like
|
# acciones son como instrucciones switch dentro del ciclo.
|
||||||
# switch statements inside the loop.
|
|
||||||
|
|
||||||
/^fo+bar$/ {
|
/^fo+bar$/ {
|
||||||
|
|
||||||
# This action will execute for every line that matches the regular
|
# Esta acción se ejecutará por cada línea que haga match con la expresión
|
||||||
# expression, /^fo+bar$/, and will be skipped for any line that fails to
|
# regular /^fo+bar$/, y será saltada por cualquier línea que no haga match.
|
||||||
# match it. Let's just print the line:
|
# Vamos a sólo mostrar la línea:
|
||||||
|
|
||||||
print
|
print
|
||||||
|
|
||||||
# Whoa, no argument! That's because print has a default argument: $0.
|
# ¡Wow, sin argumento! Eso es porque print tiene uno por defecto: $0.
|
||||||
# $0 is the name of the current line being processed. It is created
|
# $0 es el nombre de la línea actual que se está procesando.
|
||||||
# automatically for you.
|
# Se crea automáticamente para ti.
|
||||||
|
|
||||||
# You can probably guess there are other $ variables. Every line is
|
# Probablemente puedas adivinar que hay otras variables $. Cada línea es
|
||||||
# implicitely split before every action is called, much like the shell
|
# separada implícitamente antes de que se llame cada acción, justo como lo
|
||||||
# does. And, like the shell, each field can be access with a dollar sign
|
# hace shell. Y, como shell, cada campo puede ser accesado con $.
|
||||||
|
|
||||||
# This will print the second and fourth fields in the line
|
# Esto mostrará el segundo y cuarto campos de la línea
|
||||||
print $2, $4
|
print $2, $4
|
||||||
|
|
||||||
# AWK automatically defines many other variables to help you inspect and
|
# AWK automáticamente define muchas otras variables que te ayudan a
|
||||||
# process each line. The most important one is NF
|
# inspeccionar y procesar cada línea. La más importante es NF
|
||||||
|
|
||||||
# Prints the number of fields on this line
|
# Imprime el número de campos de esta línea
|
||||||
print NF
|
print NF
|
||||||
|
|
||||||
# Print the last field on this line
|
# Imprime el último campo de esta línea
|
||||||
print $NF
|
print $NF
|
||||||
}
|
}
|
||||||
|
|
||||||
# Every pattern is actually a true/false test. The regular expression in the
|
# Cada patrón es realmente un prueba de verdadero/falso. La expresión regular
|
||||||
# last pattern is also a true/false test, but part of it was hidden. If you
|
# en el último patrón también es una prueba verdadero/falso, pero parte de eso
|
||||||
# don't give it a string to test, it will assume $0, the line that it's
|
# estaba oculto. Si no le das un string a la prueba, supondrá $0, la línea que
|
||||||
# currently processing. Thus, the complete version of it is this:
|
# se está procesando. La versión completa de esto es:
|
||||||
|
|
||||||
$0 ~ /^fo+bar$/ {
|
$0 ~ /^fo+bar$/ {
|
||||||
print "Equivalent to the last pattern"
|
print "Equivalente al último patrón"
|
||||||
}
|
}
|
||||||
|
|
||||||
a > 0 {
|
a > 0 {
|
||||||
# This will execute once for each line, as long as a is positive
|
# Esto se ejecutará una vez por línea, mientras a sea positivo
|
||||||
}
|
}
|
||||||
|
|
||||||
# You get the idea. Processing text files, reading in a line at a time, and
|
# You get the idea. Processing text files, reading in a line at a time, and
|
||||||
|
Reference in New Issue
Block a user