Substituciones con Sed (Parte II)




Substitución



A continuación vamos a escribir algunos ejemplos para darle forma a estas viñetas. Para esto, realizaremos el ejercicio sobre un contenido como el siguiente.

Linux (pronounced Listeni/ˈlɪnəks/ LIN-əks[9][10] or, less frequently, /ˈlaɪnəks/ LYN-əks[10][11]) is a Unix-like computer operating system assembled under the model of free and open-source
software development and distribution. The defining component of Linux is the Linux kernel,[12] an operating system kernel first released on September 17, 1991 by Linus Torvalds.[13][14][15]
The Free Software Foundation uses the name GNU/Linux to describe the operating system, which has led to some controversy.

There are many flavors of Linux, which difference is the purposes of each one. For example, Fedora is for general purposes meanwhile Backtrack is intended for penetration testing and auditing.
example.txt


Ejercicio 1

Sustituir la primer coincidencia de la palabra Linux o linux por la palabra Tux. Notar que las demás coincidencias con el patrón no son modificadas.

sed 's/[Ll]inux/Tux/1' example.txt


Salida

Tux (pronounced Listeni/ˈlɪnəks/ LIN-əks[9][10] or, less frequently, /ˈlaɪnəks/ LYN-əks[10][11]) is a Unix-like computer operating system assembled under the model of free and open-source so
ftware development and distribution. The defining component of Linux is the Linux kernel,[12] an operating system kernel first released on September 17, 1991 by Linus Torvalds.[13][14][15] T
he Free Software Foundation uses the name GNU/Linux to describe the operating system, which has led to some controversy.



There are many flavors of Linux, which difference is the purposes of each one. For example, Fedora is for general purposes meanwhile Backtrack is intended for penetration testing and auditing.

Ejercicio 2.

Podemos usar un address seguida de una expresión regular para hace los cambios. El address nos servirá para ubicar un rango de líneas o una línea en específico sobre la cual actuar. La siguiente expresión se lee como: en la tercera línea del archivo example.txt cambia la palabra purpose por Tux únicamente en la segunda coincidencia que encuentes.

sed '3s/purposes/Tux/2' example.txt

Salida

Linux (pronounced Listeni/ˈlɪnəks/ LIN-əks[9][10] or, less frequently, /ˈlaɪnəks/ LYN-əks[10][11]) is a Unix-like computer operating system assembled under the model of free and open-source
software development and distribution. The defining component of Linux is the Linux kernel,[12] an operating system kernel first released on September 17, 1991 by Linus Torvalds.[13][14][15]
The Free Software Foundation uses the name GNU/Linux to describe the operating system, which has led to some controversy.

There are many flavors of Linux, which difference is the purposes of each one. For example, Fedora is for general Tux meanwhile Backtrack is intended for penetration testing and auditing.


Ejercicio 3.

Remplazo de metacaracteres es una tarea poco frecuente, pero que no por ello dejaremos de aprenderlo. La siguiente expresión se lee como: En la línea número 3 reemplaza la primer coincidencia de un ‘.’ por un enter.

sed -e '3s/\./\
/1' example.txt

Salida
Linux (pronounced Listeni/ˈlɪnəks/ LIN-əks[9][10] or, less frequently, /ˈlaɪnəks/ LYN-əks[10][11]) is a Unix-like computer operating system assembled under the model of free and open-source
software development and distribution. The defining component of Linux is the Linux kernel,[12] an operating system kernel first released on September 17, 1991 by Linus Torvalds.[13][14][15]
The Free Software Foundation uses the name GNU/Linux to describe the operating system, which has led to some controversy.

There are many flavors of Linux, which difference is the purposes of each one
For example, Fedora is for general purposes meanwhile Backtrack is intended for penetration testing and auditing.



Ejemplo 4.

También es posible añadir caracteres cuando se encuentra un patrón.

sed 's/\([Ll]inux\)/(&)/' example.txt

Salida
(Linux) (pronounced Listeni/ˈlɪnəks/ LIN-əks[9][10] or, less frequently, /ˈlaɪnəks/ LYN-əks[10][11]) is a Unix-like computer operating system assembled under the model of free and open-sourc
e software development and distribution. The defining component of Linux is the Linux kernel,[12] an operating system kernel first released on September 17, 1991 by Linus Torvalds.[13][14][1
5] The Free Software Foundation uses the name GNU/Linux to describe the operating system, which has led to some controversy.

There are many flavors of (Linux), which difference is the purposes of each one. For example, Fedora is for general purposes meanwhile Backtrack is intended for penetration testing and audit
ing.



Si se desea extender a todas las palabras dentro del archivo, solo basta indicare a sed que lo realice globalmente (comando g).

sed 's/\([Ll]inux\)/(&)/g' example.txt

Salida
(Linux) (pronounced Listeni/ˈlɪnəks/ LIN-əks[9][10] or, less frequently, /ˈlaɪnəks/ LYN-əks[10][11]) is a Unix-like computer operating system assembled under the model of free and open-sourc
e software development and distribution. The defining component of (Linux) is the (Linux) kernel,[12] an operating system kernel first released on September 17, 1991 by Linus Torvalds.[13][1
4][15] The Free Software Foundation uses the name GNU/(Linux) to describe the operating system, which has led to some controversy.

There are many flavors of (Linux), which difference is the purposes of each one. For example, Fedora is for general purposes meanwhile Backtrack is intended for penetration testing and audit
ing.



Ejemplo 5.

Algunas veces me he encontrado con la necedidad de cambiar el orden de las coincidencias para dar formato a las salidas, ya sea por presentación o por necesidad de integrarlo con algún script que utiliza las posiciones como datos. Solo mantener en mente que los paréntesis son necesarios, asimismo, colocar las diagonales que ‘escapan’ dichos paréntesis. También hay que recordar que un ‘.’ significa cualquier caracter y el ‘*’ es cero o más veces. En conjunto estos últimos dos caracteres se pueden leer como ‘todos los caracteres hasta antes del «:»’.

Vamos a utilizar otro archivo de entrada para dejar claro el concepto.

one:two
ten:twenty
swap.txt

sed 's/\(.*\):\(.*\)/\2:\1/' swap.txt

Salida
two:one
twenty:ten


Comandos

Sed utiliza algunos comandos para realizar impresiones o eliminación del pattern space. Hay cerca de 5 comandos con funciones distintas, al menos cuando no hablamos de multiline. A continuación vamos a ver algunos de los comandos básicos.

Ejemplo 1

Eliminar todas las líneas en blanco. Usaremos para el ejemplo, el primer archivo llamado example.txt.

sed '/^$/d' example.txt


Salida
Linux (pronounced Listeni/ˈlɪnəks/ LIN-əks[9][10] or, less frequently, /ˈlaɪnəks/ LYN-əks[10][11]) is a Unix-like computer operating system assembled under the model of free and open-source
software development and distribution. The defining component of Linux is the Linux kernel,[12] an operating system kernel first released on September 17, 1991 by Linus Torvalds.[13][14][15]
The Free Software Foundation uses the name GNU/Linux to describe the operating system, which has led to some controversy.
There are many flavors of Linux, which difference is the purposes of each one. For example, Fedora is for general purposes meanwhile Backtrack is intended for penetration testing and auditin
g.


Podría utilizarse, asimismo, las address para indicar que línea es la que deseamos afectar.

Ejemplo 2

Eliminar todas las líneas (recordar que no afectamos el archivo original) del archivo. Solo lo veo funcional cuando quiere vaciarse un archivo de una manera poco convencional en algún foro o charla que se dé, todo para hacer gala de nuestros conocimientos de sed.

sed 'd' example.txt


Ejemplo 3.

Eliminar la primer línea de un archivo.

sed -e '1d' example.txt

Salida

There are many flavors of Linux, which difference is the purposes of each one. For example, Fedora is for general purposes meanwhile Backtrack is intended for penetration testing and auditin
g




Ejemplo 4.

Eliminar un rango de líneas en un archivo. Por ejemplo, de la 1 a la 10. Para este ejemplo usaremos un archivo distinto de entrada que contiene 16 líneas de texto.


1 The Unix operating system was conceived and implemented in 1969 at AT&T's Bell Laboratories in the United States by Ken Thompson, Dennis Ritchie, Douglas McIlroy, and Joe Ossanna.[32] Fi
   rst released in 1971, Unix was written entirely in assembly language, as was common practice at the time. Later, in a key pioneering approach in 1973, it was rewritten in the C programmi
   ng language by Dennis Ritchie (with exceptions to the kernel and I/O). The availability of a high-level language implementation of Unix made its porting to different computer platforms e
   asier.
 2  
 3 Due to an earlier antitrust case forbidding it from entering the computer business, AT&T was required to license the operating system's source code to anyone who asked.[33] As a result,
   Unix grew quickly and became widely adopted by academic institutions and businesses. In 1984, AT&T divested itself of Bell Labs; freed of the legal obligation requiring free licensing, B
   ell Labs began selling Unix as a proprietary product.
 4  
 5 The GNU Project, started in 1983 by Richard Stallman, has the goal of creating a "complete Unix-compatible software system" composed entirely of free software. Work began in 1984.[34] La
   ter, in 1985, Stallman started the Free Software Foundation and wrote the GNU General Public License (GNU GPL) in 1989. By the early 1990s, many of the programs required in an operating
   system (such as libraries, compilers, text editors, a Unix shell, and a windowing system) were completed, although low-level elements such as device drivers, daemons, and the kernel were
    stalled and incomplete.[35][not in citation given]
 6  
 7 Linus Torvalds has stated that if the GNU kernel had been available at the time (1991), he would not have decided to write his own.[36]
 8  
 9 Although not released until 1992 due to legal complications, development of 386BSD, from which NetBSD, OpenBSD and FreeBSD descended, predated that of Linux. Torvalds has also stated tha
   t if 386BSD had been available at the time, he probably would not have created Linux.[37]
10  
11 MINIX was created by Andrew S. Tanenbaum, a computer science professor, and released in 1987 as a minimal Unix-like operating system targeted at students and others who wanted to learn t
   he operating system principles. Although the complete source code of MINIX was freely available, the licensing terms prevented it from being free software until the licensing changed in
   April 2000.[38]
12 Creation
13  
14 In 1991, while attending the University of Helsinki, Torvalds became curious about operating systems[39] and frustrated by the licensing of MINIX, which at the time limited it to educati
   onal use only.[38] He began to work on his own operating system kernel, which eventually became the Linux kernel.
15  
16 Torvalds began the development of the Linux kernel on MINIX and applications written for MINIX were also used on Linux. Later, Linux matured and further Linux kernel development took pla
   ce on Linux systems.[40] GNU applications also replaced all MINIX components, because it was advantageous to use the freely available code from the GNU Project with the fledgling operati
   ng system; code licensed under the GNU GPL can be reused in other computer programs as long as they also are released under the same or a compatible license. Torvalds initiated a switch
   from his original license, which prohibited commercial redistribution, to the GNU GPL.[41] Developers worked to integrate GNU components with the Linux kernel, making a fully functional
   and free operating system.[35]
lines.txt



sed '1,10d' lines.txt


Salida
MINIX was created by Andrew S. Tanenbaum, a computer science professor, and released in 1987 as a minimal Unix-like operating system targeted at students and others who wanted to learn the o
perating system principles. Although the complete source code of MINIX was freely available, the licensing terms prevented it from being free software until the licensing changed in April 20
00.[38]
Creation

In 1991, while attending the University of Helsinki, Torvalds became curious about operating systems[39] and frustrated by the licensing of MINIX, which at the time limited it to educational
use only.[38] He began to work on his own operating system kernel, which eventually became the Linux kernel.

Torvalds began the development of the Linux kernel on MINIX and applications written for MINIX were also used on Linux. Later, Linux matured and further Linux kernel development took place o
n Linux systems.[40] GNU applications also replaced all MINIX components, because it was advantageous to use the freely available code from the GNU Project with the fledgling operating syste
m; code licensed under the GNU GPL can be reused in other computer programs as long as they also are released under the same or a compatible license. Torvalds initiated a switch from his ori
ginal license, which prohibited commercial redistribution, to the GNU GPL.[41] Developers worked to integrate GNU components with the Linux kernel, making a fully functional and free operati
ng system.[35]


Conclusión



Con estos ejemplos concluimos la segunda fase de utilización de sed. No son todos los comandos que podemos utilizar, sin embargo, estaré abordando los otros comandos en la siguiente entrega de esta serie de escritos sobre el tema.


Espero que encuentren de utilidad y redactados de manera clara cada uno de los ejemplos a modo que puedan replicarlos y aprender algunos usos.

Comentarios

Entradas populares