一,指令
-
more指令 —— 分页显示文件内容
-
less指令 —— 可以向前或向后查看文件内容
-
head指令 —— 查看文件开头的内容
-
tail指令 —— 显示文件尾部的内容
-
cat指令 —— 显示文件内容
练习
1、查看test文件的前5行内容(3种方式)
head -5 test
head -n5 test
cat -n test | head -n5
more
2、查看test文件的第9行和第10行内容(文件一共10行,有几种方式?)
tail -n2 test
cat test | head | tail -n2
head test | tail -n -2
head
3、查看test文件的前5行内容并输出到文件test1
head -n5 test >>test1
cat test | head -5 >>test1
cat test;head -5 test >>test1
4、统计出/etc/passwd文件中以login结尾的有多少行
grep “login
"
/
e
t
c
/
p
a
s
s
w
d
∣
w
c
−
l
g
r
e
p
−
c
"
l
o
g
i
n
" /etc/passwd | wc -l grep -c "login
"/etc/passwd∣wc−lgrep−c"login” /etc/passwd
5、查找出test文件中包含linux的行并显示行号(2种方式)
cat -n test | grep “linux”
grep -n “linux” test
tail -n +1 test | cat -n | grep “linux”
6、过滤出以#开头和空行的行
grep -vE “#|$” test
7、统计文件test中linux出现的次数
grep -o “linux” test|wc -l
grep -o “linux” test|wc -w
grep -o “linux” test | grep -c “linux”
8、查找出/etc/passwd文件中包含user或者used的行
grep -E “user|used” /etc/passwd
grep “user|used” /etc/passwd
grep “use[rd]” /etc/passwd
9、统计root或者Root在test文件中共匹配到多少行
grep -i “root” test | wc -l
grep -ic “root” test
grep -E “root|Root” test
grep “[rR]oot” test
10、查看/dev目录下以sd开头和中间包含a字母的文件个数
ls /dev | grep “^sd” | grep “a” | wc -l
思考题:统计/opt目录下的文件大小并升序/降序排列
du -a /opt | sort -nr
如何用grep匹配有
的
行
/
以
的行/以
的行/以结尾的行
匹配有
的
行
:
g
r
e
p
"
[
的行: grep "[
的行:grep"[]" test
grep “\$” test
以
结
尾
的
行
g
r
e
p
"
[
结尾的行 grep "[
结尾的行grep"[]$" test
grep “$$” test
\:转义字符