简单的文本处理
2017-01-10 本文已影响13人
EldonZhao
一、tr命令
tr
命令可以用来删除一段文本中的某些文字,或者将其进行转换。
-
tr
使用方法:
Usage: tr [OPTION]... SET1 [SET2]
Translate, squeeze, and/or delete characters from standard input,
writing to standard output.
-
tr
常用选项:
选项 | 说明 |
---|---|
-d |
删除和SET1匹配的字符,注意不是全词匹配也不是按字符顺序匹配 |
-s |
去除SET1指定的在输入文本中连续并重复的字符 |
-
tr
常见的使用方法:
shiyanlou:~/ $ echo 'hello 2e31ldon' | tr -d '[:digit:]'
hello eldon
shiyanlou:~/ $ echo 'hello eeldon' | tr -s '[:digit:]'
hello eeldon
shiyanlou:~/ $ echo 'hello eeldon' | tr -s '[:alnum:]'
helo eldon
shiyanlou:~/ $ echo 'hello eldon' | tr '[:upper:]' '[:lower:]'
hello eldon
shiyanlou:~/ $ echo 'heLlo ElDon' | tr '[:upper:]' '[:lower:]'
hello eldon
二、col命令
col
命令可以将Tab
换成对等数量的空格键,或反转这个操作。
-
col
使用方法:
col [option]
-
col
常用选项:
选项 | 说明 |
---|---|
-x |
将Tab 转换为空格 |
-h |
将空格转换为Tab
|
-
col
常见使用方法:
shiyanlou:~/ $ cat eldon.log | col -x > eldon1.log
shiyanlou:~/ $ cat /etc/protocols| col -x | cat -A
三、jion命令
join
命令就是讲两个文件中包含相同内容的那一行合并在一起。
-
join
使用方法:
Usage: join [OPTION]... FILE1 FILE2
For each pair of input lines with identical join fields, write a line to
standard output. The default join field is the first, delimited
by whitespace. When FILE1 or FILE2 (not both) is -, read standard input.
-
join
常用选项:
选项 | 说明 |
---|---|
-t |
指定分隔符,默认为空格 |
-i |
忽略大小写的差异 |
-1 |
指明第一个文件要用哪个字段来对比,默认对比第一个字段 |
-2 |
指明第二个文件要用哪个字段来对比,默认对比第一个字段 |
-
join
常见使用方法:
shiyanlou:~/ $ sudo join -t ':' /etc/passwd /etc/shadow
四、paste命令
paste
命令与join
类似,在不对比数据的情况下,简单的将多个文件合并在一起,以Tab
隔开。
-
paste
使用方法:
Usage: paste [OPTION]... [FILE]...
Write lines consisting of the sequentially corresponding lines from
each FILE, separated by TABs, to standard output.
With no FILE, or when FILE is -, read standard input.
-
paste
常用选项:
选项 | 说明 |
---|---|
-d |
指定分隔符,默认为Tab
|
-s |
不合并到一行 |
-
paste
常见使用方法:
shiyanlou:~/ $ sudo paste /etc/passwd /etc/shadow | head -n 2
root:x:0:0:root:/root:/bin/bashroot:*:16583:0:99999:7:::
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologindaemon:*:16583:0:99999:7:::
shiyanlou:~/ $ sudo paste -d "&" /etc/passwd /etc/shadow | head -n 2
root:x:0:0:root:/root:/bin/bash&root:*:16583:0:99999:7:::
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin&daemon:*:16583:0:99999:7:::
shiyanlou:~/ $
轻松一刻:
shiyanlou:~/ $ sudo apt-get install ninvaders
shiyanlou:~/ $ ninvaders