文本查看类命令

文本查看类命令

1.cat命令

文本内容输出(查看)
cat [选项] 文件名

[root@localhost test]# cat hello.txt 
hello!!!!
hello!!!!
hello!!!!
haha!!!
haha!!!

1.1 查看文本(显示行数)

[root@localhost test]# cat -n hello.txt 
	 1	hello!!!!
	 2	hello!!!!
	 3	hello!!!!
	 4	haha!!!
	 5	haha!!!

逆向输出

[root@localhost test]# tac hello.txt 
haha!!!
haha!!!
hello!!!!
hello!!!!
hello!!!!

1.2 连接文件(文本文件)共同输出

直接连接

[root@localhost test]# cat hello.txt  test.txt 
hello!!!!
hello!!!!
hello!!!!
haha!!!
haha!!!
this test!!
test!

使用重定向

[root@localhost test]# cat hello.txt  test.txt > test1.txt 
[root@localhost test]# cat test1.txt 
hello!!!!
hello!!!!
hello!!!!
haha!!!
haha!!!
this test!!
test!

1.3 here document 生成固定内容的文本
<< 输入重定向


2.more命令 分页显示

回车键向下逐行滚动
空格向下翻一屏
q退出 quit
less命令 类似于more命令
可按b向上翻屏
可用pageup和pagedown,但不会自动退出


3.head命令

查看文件的开头一部分内容(默认前10行)

[root@localhost home]# head passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

head -number 前(number行)

[root@localhost home]# head -n 2 passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@localhost home]# head -2 passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

结合cat输出

[root@localhost home]# cat -n passwd | head
     1	root:x:0:0:root:/root:/bin/bash
     2	bin:x:1:1:bin:/bin:/sbin/nologin
     3	daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4	adm:x:3:4:adm:/var/adm:/sbin/nologin
     5	lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     6	sync:x:5:0:sync:/sbin:/bin/sync
     7	shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     8	halt:x:7:0:halt:/sbin:/sbin/halt
     9	mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10	operator:x:11:0:operator:/root:/sbin/nologin

4.tail命令

查看文件的末尾一部分内容(默认后10行)
tail [选项] 文件名

[root@localhost home]# tail passwd 
sssd:x:996:993:User for sssd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
rngd:x:995:992:Random Number Generator Daemon:/var/lib/rngd:/sbin/nologin
dasdada9:x:1006:1006::/home/dasdada9:/bin/bash
dasdada10:x:1007:1007::/home/dasdada10:/bin/bash
halo:x:1008:1008::/home/halo:/bin/bash
yalin:x:1009:1009::/home/yalin:/bin/bash
califeng:x:1010:1010::/home/califeng:/bin/bash
cali123:x:1011:1011::/home/cali123:/bin/bash
chrony:x:994:991::/var/lib/chrony:/sbin/nologin

结合cat输出

[root@localhost home]# cat -n passwd | tail
    20	sssd:x:996:993:User for sssd:/:/sbin/nologin
    21	sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    22	rngd:x:995:992:Random Number Generator Daemon:/var/lib/rngd:/sbin/nologin
    23	dasdada9:x:1006:1006::/home/dasdada9:/bin/bash
    24	dasdada10:x:1007:1007::/home/dasdada10:/bin/bash
    25	halo:x:1008:1008::/home/halo:/bin/bash
    26	yalin:x:1009:1009::/home/yalin:/bin/bash
    27	califeng:x:1010:1010::/home/califeng:/bin/bash
    28	cali123:x:1011:1011::/home/cali123:/bin/bash
    29	chrony:x:994:991::/var/lib/chrony:/sbin/nologin

tail -n +number 从第number行开始显示到末尾

[root@localhost home]# cat -n passwd  | tail -n +25
    25	halo:x:1008:1008::/home/halo:/bin/bash
    26	yalin:x:1009:1009::/home/yalin:/bin/bash
    27	califeng:x:1010:1010::/home/califeng:/bin/bash
    28	cali123:x:1011:1011::/home/cali123:/bin/bash
    29	chrony:x:994:991::/var/lib/chrony:/sbin/nologin

-f 选项
动态的显示追加进来的内容
运用场合:一直盯着某个文件看,看日志文件的变化,根据变化的内容去排除故障


5.sed命令

sed是liunx里的文本查看和替换的命令
根据行号来查找
1p 输出第一个行

[root@localhost home]# cat -n passwd | sed -n '3p'
     3	daemon:x:2:2:daemon:/sbin:/sbin/nologin

1p;3p;5p ;是命令连接符号,执行多个命令

[root@localhost home]# cat -n passwd | sed -n '3p;9p;15p'
     3	daemon:x:2:2:daemon:/sbin:/sbin/nologin
     9	mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    15	systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin

-n 输出符号要求的行,不符合要求的不显示出来


6.wc命令

统计每个文件的多少行,多少单词,多少字节
-l 统计行数

[root@localhost home]# cat passwd | wc -l
29

-L 统计一行中最多的字符(一个文件里最长的行的字符数)

[root@localhost home]# echo dasjkfhasjksuifhasjk| wc -L
20

7.文本三剑客 grep,sed,awk

7.1grep:过滤,查找

[root@localhost home]# cat passwd | grep halo
halo:x:1008:1008::/home/halo:/bin/bash
[root@localhost home]# df -Th |grep "/$"
/dev/mapper/cl-root xfs        17G  2.3G   15G   14% /
"/$" 意思是以/结尾  $为结束符号

7.2sed:替换
-i直接对源文件进行操作

[root@localhost home]# cat hello.txt 
abc efg efg
abc efg efg
abc efg efg
abc efg efg
[root@localhost home]# sed -i 's/abc/nihao/g' hello.txt 
[root@localhost home]# cat hello.txt 
nihao efg efg
nihao efg efg
nihao efg efg
nihao efg efg
若更改efg出现的第二个位置
[root@localhost home]# sed -i 's/efg/abc/2' hello.txt 
[root@localhost home]# cat hello.txt 
nihao efg abc
nihao efg abc
nihao efg abc
nihao efg abc

7.3awk:文本截取命令

[root@localhost home]# df -Th | grep "/$"
/dev/mapper/cl-root xfs        17G  2.3G   15G   14% /
[root@localhost home]# df -Th | grep "/$" | awk '{print $3,$6}'
17G 14%
[root@localhost home]# df -Th | grep "/$" | awk '{print $3}'
17G
字段:一段文字(一列)
$3 第三个字段
默认的字段分隔符是空白(空格、tab、回车)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值