Bash Commands - tr cat tac

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值