Linux常用命令(4)——文本文件编辑命令

cat&more

cat查看纯文本文件(内容较少)。cat [参数] 文件名称
more查看纯文本文件(内容较多)。more [参数] 文件名称’`

[root@hostname ~]# cat -n yum.conf
[root@hostname ~]# more yum.conf 	#分页显示

head&tail

head查看纯文本文件前N行。head [参数] 文件名称
tail查看纯文本文件后N行或持续刷新文件最新内容。tail [参数] 文件名称

[root@hostname ~]# head -n 10 logrotate.conf  #显示前10行
[root@hostname ~]# tail -n 10 logrotate.conf  #显示后10行
[root@hostname ~]# tail -f hawkey.log     #持续刷新文件内容,常用于查日志

tr

替换文本内容中的字符。tr [原始字符][目标字符]

[root@hostname ~]# tr [a-z] [A-Z] < a.txt  #将a.txt中小写字符转换成大写输出到终端
[root@hostname ~]# tr -d [0-9] < a.txt	 #删除a.txt中所有数字并输出到终端

wc

统计指定文本文件的行数。wc [参数] 文件名称

[root@hostname ~]# wc -l /etc/passwd
45 /etc/passwd

stat

查看文件具体存储细节和时间等信息。stat 文件名称

[root@hostname ~]# stat passwd
  File: passwd
  Size: 2499      	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 17832574    Links: 1
Access: (0777/-rwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:passwd_file_t:s0
Access: 2023-04-14 16:55:57.496076586 +0800         #文件最后依次被访问时间
Modify: 2023-04-12 00:42:07.396012983 +0800			#内容最后一次被修改时间
Change: 2023-04-14 16:55:46.956076359 +0800			#文件属性最后依次被修改时间
 Birth: -

grep

按行提取文本内容。 grep [参数] 文件名称

[root@hostname ~]# grep root /etc/passwd    #搜索passwd文件中包含root的内容行
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

[root@hostname ~]# grep ^root /etc/passwd	#搜索passwd文件中以root开头的行
root:x:0:0:root:/root:/bin/bash

[root@hostname ~]# grep root /etc/passwd /etc/shadow #搜索passwd、shadow文件中包含root的内容行
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
/etc/shadow:root:$6$iwAobTDoE5C656YC$BCbK0OPPf22wmfwDerF/NwT8kWbKRLRsloBrW8GHawNqhVsWs8Bi6B1lA12Zf7hFWPxjgeUv.nZTlVN2SQ5v71:19458:0:99999:7:::

[root@hostname ~]# grep -c root /etc/passwd /etc/shadow #搜索passwd、shadow文件中包含root内容行的数量 
/etc/passwd:2
/etc/shadow:1

[root@hostname ~]# grep -v nologin /etc/passwd   #显示不包含nologin的行,此条找到允许登录的账户
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
linuxprobe:x:1000:1000:linuxprobe:/home/linuxprobe:/bin/bash

[root@hostname ~]# grep -l root *     #搜索当前目录中包含root的文件,未找到提示
anaconda-ks.cfg
backup.txt
grep: Desktop: Is a directory
grep: Documents: Is a directory
grep: Downloads: Is a directory
initial-setup-ks.cfg
grep: Music: Is a directory
grep: Pictures: Is a directory
grep: Public: Is a directory
grep: Templates: Is a directory
grep: Videos: Is a directory

[root@hostname ~]# grep -sl root *  #搜索当前目录中包含root的文件,未找到不提示
anaconda-ks.cfg
initial-setup-ks.cfg

[root@hostname ~]# grep -srl root /etc  #搜索etc文件及其子文件中含root的文件

#精确匹配到指定词
[root@hostname ~]# grep -x cd anaconda-ks.cfg     #没有cd
[root@hostname ~]# grep -x cdrom  anaconda-ks.cfg #有cdrom
cdrom

cut

按“列”提取文本内容。cut [参数] 文件名称

[root@hostname ~]# cut -d : -f 1 /etc/passwd  #以冒号为分割符,提取第一列内容
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
....................省略.............

diff

比较多个文件内容差异。 diff [参数] 文件名称1 文件名称2

[root@hostname ~]# diff --brief a.txt b.txt   #判断是否相同
[root@hostname ~]# diff -c a.txt b.txt   #比较不同,定位所在行数

uniq

去除文本中连续的重复行。 uniq [参数] 文件名称

[root@hostname ~]# uniq a.txt
[root@hostname ~]# uniq -c a.txt  #统计重复内容出现次数

sort

对文本内容进行排序。 sort [参数] 文件名称

[root@hostname ~]# sort a.txt  #按照字母
[root@hostname ~]# sort -t : -k 3 -n a.txt #以冒号为间隔,对a.txt按照数字大小对第3列进行排序
#-t指定间隔符为:,-k指定列为3,-n指定为数字排序

参考

《Linux就该这么学》

猜你喜欢

转载自blog.csdn.net/weixin_47505548/article/details/130204138