background preloader

Tr

Facebook Twitter

BashFAQ/083. There are several ways to test for a defined or non-empty variable or function depending upon the requirements.

BashFAQ/083

"Declared", "Defined", and "Undefined" Like in many languages, declaration and definition can be independent steps. To declare an object is to give it a name and datatype, whereas a definition associates it with a value, which may be empty. In Unix shell terminology, "unset", "set", and "null" are often used to mean "undefined", "defined", and "empty" respectively. Since "string" is very nearly the only common datatype, "null" usually means the empty string. Testing for a defined, non-empty variable / parameter.

Since the most common goal is to distinguish between an empty and non-empty variable or parameter, we'll discuss it first. To check the converse, whether a variable is empty (unset or zero-length), invert the above logic. test x"$var" = x test -z "$var" [ -z "$var" ] test ! Testing that a variable has been declared # Bash/ksh [[ ${var+_} ]] Bash 4.2 adds a -v test: Understanding Linux / UNIX tr command. Q.

Understanding Linux / UNIX tr command

Can you explain the tr command and how to use it under Linux / UNIX like oses? A. The tr utility copies the given input to produced the output with substitution or deletion of selected characters. tr abbreviated as translate or transliterate. It takes as parameters two sets of characters, and replaces occurrences of the characters in the first set with the corresponding elements from the other set i.e. it is used to translate characters. It is commonly used in shell scripts and other application. Tr Man Page. Translate, squeeze, and/or delete characters.

tr Man Page

Syntax tr [options]... SET1 [SET2] `tr' copies standard input to standard output, performing one of the following operations: * translate, and optionally squeeze repeated characters in the result,* squeeze repeated characters,* delete characters,* delete characters, then squeeze repeated characters from the result. The SET1 and (if given) SET2 arguments define ordered sets of characters, referred to below as SET1 and SET2. Specifying sets of characters The format of the SET1 and SET2 arguments resembles the format of regular expressions; however, they are not regular expressions, only lists of characters.

Backslash escapes A backslash followed by a character not listed below causes an error message. \a Control-G. The notation M-N expands to all of the characters from M through N, in ascending order. Repeated characters The notation [C*N] in SET2 expands to N copies of character C. Character classes. Comando tr (unix): convertir mayúsculas a minúsculas y viceversa. Un truco rápido con el comando Unix tr, vamos a convertir las letras mayúsculas a minúsculas de un fichero.

Comando tr (unix): convertir mayúsculas a minúsculas y viceversa

Fichero test.txt: $ cat test.txt minusculas MAYUSCULAS Convertir mayúsculas en minúsculas del fichero test.txt: $ cat test.txt | tr [:upper:] [:lower:] minusculas mayusculas Convertir minúsculas en mayúsculas del fichero test.txt: $ cat test.txt | tr [:lower:] [:upper:] MINUSCULAS MAYUSCULAS Por supuesto, el comando tr nos ofrece muchas más opciones para transformación de carácteres, os dejo la ayuda del comando, recordad que en las páginas man hay mucha más información. $ tr --help Modo de empleo: tr [OPCIÓN]... También te puede interesar: Cómo dividir archivos en sistemas Unix con split El comando split en sistemas Unix sirve para partir/dividir un archivo en varios de menor tamaño.