Linux基本命令(三)

文件过滤和内容编辑命令
1、cat 查看文件内容 或 合并文件
cat>file.txt 后面接要写的内容,ctrl+d结束输入
cat>>file.txt<<EOF
hello,world!
EOF
cat /dev/null >file.txt
参数:-n行号 -b忽略空白行行号 -E把行尾隐藏的结束标识符$显示出来
cat >test.txt<<-EOF
hello,world!
EOF
#如果使用了-EOF,结尾的EOF可以不用顶格写
利用cat在脚本中显示帮助菜单
#!/bin/bash
menu()
{
cat <<END
usage:$0 {start|stop|status|reboot}
END
}
menu

2、more 分页显示文件内容
more -5 /etc/passwd
more +10 /etc/passwd 从第10行开始
3、less -N显示行号
4、head 显示文件头部内容,默认前10行
5、tail 显示文件尾部内容
6、tailf 跟踪日志文件
7、cut 从文本中截取一段文字并输出
参数:-b以字节为单位 -c以字符为单位 -d自定义分隔符 -f指定显示哪个区域
[root@localhost dir]# cat test.txt
I learn linux command and shell my qq is 123456789
[root@localhost dir]# cut -b 4 test.txt
e
[root@localhost dir]# cut -b 3-7 test.txt
learn
[root@localhost dir]# cut -b 3-7,9-13 test.txt
learnlinux
[root@localhost dir]# cut -b 3- test.txt
learn linux command and shell my qq is 123456789
[root@localhost dir]# cut -b -13 test.txt
I learn linux
[root@localhost dir]# cut -d: -f 1 /etc/passwd |tail -5
mysql
gdm
sshd
nslcd
tcpdump
[root@localhost dir]# cut -d: -f 3-5 /etc/passwd |head -5
0:0:root
1:1:bin
2:2:daemon
3:4:adm
4:7:lp

8、split 分割文件,可以按照指定的行数或指定的文件大小分割文件
9、paste 合并文件
10、sort 文本排序
参数:-n按数值大小排序 -u去重复行 -t指定分隔符 -k按指定区域排序 -r倒序排列
默认以行为单位进行排序
[root@localhost dir]# cat>3.txt
2
33
57
18
49
4
79
0
23
[root@localhost dir]# sort 3.txt
0
18
2
23
33
4
49
57
79
[root@localhost dir]# sort -n 3.txt
0
2
4
18
23
33
49
57
79
[root@localhost dir]# sort -nr 3.txt
79
57
49
33
23
18
4
2
0
[root@localhost dir]# cat 3.txt
2
33
57
18
49
4
79
0
23
33
4
4
49
33
[root@localhost dir]# cat 3.txt |sort -n
0
2
4
4
4
18
23
33
33
33
49
49
57
79
[root@localhost dir]# cat 3.txt |sort -nu
0
2
4
18
23
33
49
57
79
[root@localhost dir]# cat>4.txt
23 b
4 g
17 a
6 i
88 f
9 r
[root@localhost dir]# sort 4.txt
17 a
23 b
4 g
6 i
88 f
9 r
默认以第一列进行排序
[root@localhost dir]# sort -t " " -k2 4.txt
17 a
23 b
88 f
4 g
6 i
9 r
-t指定以空格作为分隔符,-k2指定按第2列进行排序
[root@localhost dir]# sort -t " " -k1 4.txt
17 a
23 b
4 g
6 i
88 f
9 r
[root@localhost dir]# sort -t " " -k1 -n 4.txt
4 g
6 i
9 r
17 a
23 b
88 f

11、uniq 去除重复行,常用sort先排序,然后uniq去重
参数:-c显示重复行出现的次数 -u只显示唯一的行 -d只显示重复的行
[root@localhost dir]# cat 3.txt
2
33
57
18
49
4
79
0
23
33
4
4
49
33
[root@localhost dir]# uniq -c 3.txt
1 2
1 33
1 57
1 18
1 49
1 4
1 79
1 0
1 23
1 33
2 4
1 49
1 33
[root@localhost dir]# uniq -d 3.txt
4
你会发现uniq只能对相邻的重复行进行去重操作
[root@localhost dir]# sort -n 3.txt |uniq
0
2
4
18
23
33
49
57
79

12、wc 统计文件的行数,单词数,字符数 参数 -l
[root@localhost dir]# wc /etc/inittab
26 149 884 /etc/inittab
13、iconv 转换文件的编码格式
14、dos2unix
15、diff 比较两个文件的不同
16、vimdiff可视化比较工具
17、tr 替换或删除字符,从标准输入中替换,缩减或删除字符,并将结果写到标准输出
[root@localhost dir]# cat tr.txt
my name is linux shell
my qq is 123456
[root@localhost dir]# tr ‘qq‘ ‘QQ‘<tr.txt
my name is linux shell
my QQ is 123456
[root@localhost dir]# tr ‘abc‘ ‘xyz‘ <tr.txt
my nxme is linux shell
my qq is 123456
[root@localhost dir]# tr ‘[a-z]‘ ‘[A-Z]‘<tr.txt
MY NAME IS LINUX SHELL
MY QQ IS 123456
[root@localhost dir]# tr ‘[0-9]‘ ‘[a-j]‘<tr.txt
my name is linux shell
my qq is bcdefg
删除文件中出现的linux中的每个字符
[root@localhost dir]# tr -d ‘linux‘ <tr.txt
my ame s she
my qq s 123456
删除文件中的制表符和换行符
[root@localhost dir]# tr -d ‘\n\t‘ <tr.txt
[root@localhost dir]# echo ‘ooooooooldmannnnnnnnn‘ | tr -s oldman
oldman
[root@localhost dir]# tr ‘0-9‘ ‘*‘ <tr.txt
my name is linux shell
my qq is **
[root@localhost dir]# tr -c ‘0-9‘ ‘*‘ <tr.txt
****123456*[root@localhost dir]#

18、tee多重定向
tee命令用于将数据重定向到文件,同时提供一份重定向数据的副本作为后续命令的标准输入
[root@localhost dir]# ls|tee ls.txt
1.txt
2.txt
3.txt
4.txt
dir1
dir2
dir3
ip.txt
test.exe
test.txt
tr.txt
[root@localhost dir]# cat ls.txt
1.txt
2.txt
3.txt
4.txt
dir1
dir2
dir3
ip.txt
test.exe
test.txt
tr.txt
屏幕上输出的同时将结果也输出到文件
ls | tee -a ls.txt 追加内容到文件中

19、vi编辑器
普通模式 编辑模式 命令模式
G 将光标移到文件最后一行
gg 将光标移到文件第一行
0 将光标从所在位置移到当前行首
$ 将光标从所在位置移到当前行尾
n<enter> n为数字,将光标从当前行向下移动n行
ngg 移动到文件的第n行
上下左右方向键
/string 向下搜索
?string 向上搜索
n
N
yy 复制光标所在行
nyy 复制光标开始向下n行
p/P 粘贴
dd 删除光标所在行
ndd
u 恢复前一个执行过的动作
x 向后删除字符
X 向前删除字符
d1G 删除当前行至第一行
dG 删除当前行至最后一行
d0 删除当前光标文本至行首
d$ 删除当前光标文件至行尾
i o r a esc
:wq
:wq!
:q!
:set nu
:set nonu
:n1,n2s/A/B/gc 在n1行和n2行之间寻找A,用B替换
:%s/A/B/g 把符合A的内容全部替换为B,斜线为分隔符

Linux基本命令(三)

上一篇:rsync+inotify实时同步案例


下一篇:powershell--脚本运行权限政策及‘禁止执行脚本’解决方法