Bash Commands - tr cat tac

来源:互联网 发布:以公司名义开淘宝店 编辑:程序博客网 时间:2024/05/18 18:18

1. tr

character translation filter.

1.1

Either tr "A-Z" "*" <filename or tr A-Z \* <filename

changes all the uppercase letters in filename to asterisks (writes to stdout). On some systems this may not work, but tr A-Z '[**]' will.

1.2

The -d option deletes a range of characters.

echo "abcdef"                            # abcdef

echo "abcdef" | tr -d b-d          # aef

tr -d 0-9 <filename                     # Deletes all digits from the file "filename".

1.3

The --squeeze-repeats (or -s) option deletes all but the first instance of a string of
consecutive characters. This option is useful for removing excess whitespace.
echo "X3XXXX" | tr --squeeze-repeats 'X'                         # X3X

1.4

The -c "complement" option inverts the character set to match. With this option, tr acts only upon
those characters not matching the specified set.
bash$ echo "acfdeb123" | tr -c b-d +                                  # +c+d+b++++


2. cat

cat, an acronym for concatenate, lists a file to stdout. When combined with redirection (> or >>), it
is commonly used to concatenate files

 2.1

cat filename                                                 # Lists the file.
cat file.1 file.2 file.3 > file.123                  # Combines three files into one.

2.2

The -n option to cat inserts consecutive numbers before all lines of the target file(s).

The -b option numbers only the non-blank lines.

The -v option echoes nonprintable characters, using ^ notation.

The -s option squeezes multiple consecutive blank lines into a single blank line.

2.3

In a pipe, it may be more efficient to redirect the stdin to a file, rather than to cat the file.

cat filename | tr a-z A-Z
tr a-z A-Z < filename
                             # Same effect, but starts one less process, and also dispenses with the pipe.


3. tac

tac, is the inverse of cat, listing a file backwards from its end.

3.1

tac errorfix

c

b

a

cat errorfix

a

b

c