0%

xxd - make a hex dump or do the reverse

1
2
3
xxd -h
xxd [options][infile[outfile]]
xxd -r [options][infile[outfile]]
  1. -b:以2进制格式输出

    1
    2
    3
    4
    $ xxd -b train.ini
    0000000: 01011011 01110100 01110010 01100001 01101001 01101110 [train
    0000006: 01011101 00001101 00001010 01101110 01100111 01110010 ]..ngr
    000000c: 01100001 01101101 01100110 01101001 01101100 01100101 amfile
  1. -c:每行输出多少个字节

    1
    2
    3
    4
    $ xxd -c4 train.ini
    0000000: 5b74 7261 [tra
    0000004: 696e 5d0d in].
    ....
  2. -g:几个字节组成一组

    1
    2
    3
    4
    $ xxd -g4 train.ini
    0000000: 5b747261 696e5d0d 0a6e6772 616d6669 [train]..ngramfi
    0000010: 6c653d22 6e675f32 772e7478 74220d0a le="ng_2w.txt"..
    0000020: 69746572 636f756e 743d3430 30300d0a itercount=4000..
  3. -i:将文件内容输出成 c的一个数组格式

    1
    2
    3
    4
    5
    6
    $ xxd -i train.ini
    unsigned char train_ini[] = {
    0x5b, 0x74, 0x72, 0x61, 0x69, 0x6e, 0x5d, 0x0d, 0x0a, 0x6e, 0x67, 0x72,
    0x61, 0x6d, 0x66, 0x69, 0x6c, 0x65, 0x3d, 0x22, 0x6e, 0x67, 0x5f, 0x32,
    };
    unsigned int train_ini_len = 148;
  4. -l:输出几个字节后结束

    1
    2
    $ xxd -l12 train.ini
    0000000: 5b74 7261 696e 5d0d 0a6e 6772 [train]..ngr
  5. -p:以一个整块输出所有的hex, 不使用空格进行分割

    1
    2
    3
    4
    5
    6
    $ xxd -p train.ini
    5b747261696e5d0d0a6e6772616d66696c653d226e675f32772e74787422
    0d0a69746572636f756e743d343030300d0a73766d6761703d302e303030
    310d0a74656d706469723d2274656d70220d0a7664636c6973743d227664
    632e7478742220204f520d0a77686974656c6973743d2277686974652e6c
    697374220d0a626c61636b6c6973743d22626c61636b2e6c69737422
  6. -r:反转操作,将16进制转为2进制

  7. -s [+][-]seek:从第几个字符开始

    1
    2
    3
    4
    $ xxd -s5 train.ini
    0000005: 6e5d 0d0a 6e67 7261 6d66 696c 653d 226e n]..ngramfile="n
    0000015: 675f 3277 2e74 7874 220d 0a69 7465 7263 g_2w.txt"..iterc
    0000025: 6f75 6e74 3d34 3030 300d 0a73 766d 6761 ount=4000..svmga
  8. -u:输出采用大写

rev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@localhost ~]# cat iptables.bak
# Generated by iptables-save v1.3.5 on Thu Dec 26 21:25:15 2013
*filter
:INPUT DROP [48113:2690676]
:FORWARD accept [0:0]
:OUTPUT ACCEPT [3381959:1818595115]
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A OUTPUT -o lo -j ACCEPT
COMMIT
# Completed on Thu Dec 26 21:25:15 2013

[root@localhost ~]# rev iptables.bak
3102 51:52:12 62 ceD uhT no 5.3.1v evas-selbatpi yb detareneG #
retlif*
]6760962:31184[ PORD TUPNI:
]0:0[ TPECCA DRAWROF:
]5115958181:9591833[ TPECCA TUPTUO:
TPECCA j- ol i- TUPNI A-
TPECCA j- 22 tropd-- pct m- pct p- TUPNI A-
TPECCA j- 08 tropd-- pct m- pct p- TUPNI A-
TPECCA j- DEHSILBATSE,DETALER etats-- etats m- TUPNI A-
TPECCA j- pmci p- TUPNI A-
TPECCA j- ol o- TUPTUO A-
TIMMOC
3102 51:52:12 62 ceD uhT no detelpmoC #

tr

1
tr -c -d -s ["string1_to_translate_from"] ["string2_to_translate_to"] < input-file

-c 用字符串1中字符集的补集替换此字符集,要求字符集为ASCII。
-d 删除字符串1中所有输入字符。
-s 删除所有重复出现字符序列,只保留第一个;即将重复出现字符串压缩为一个字符串。
input-file是转换文件名。虽然可以使用其他格式输入,但这种格式最常用。

字符范围
指定字符串1或字符串2的内容时,只能使用单字符或字符串范围或列表。
[a-z] a-z内的字符组成的字符串。
[A-Z] A-Z内的字符组成的字符串。
[0-9] 数字串。
\octal 一个三位的八进制数,对应有效的ASCII字符。
[O*n] 表示字符O重复出现指定次数n。因此[O*2]匹配OO的字符串。

tr中特定控制字符的不同表达方式
速记符含义八进制方式
\a Ctrl-G 铃声\007
\b Ctrl-H 退格符\010
\f Ctrl-L 走行换页\014
\n Ctrl-J 新行\012
\r Ctrl-M 回车\015
\t Ctrl-I tab键\011
\v Ctrl-X \030

-EOF-