Linux文本编辑命令

2020-08-31 10:14:27 浏览数 (1)

1.cat

用于查看纯文本文件

格式:cat[选项][文件]

代码语言:javascript复制
[root@linuxprobe ~]# cat -n initial-setup-ks.cfg
     1	#version=RHEL7
     2	# X Window System configuration information
     3	xconfig  --startxonboot
     4	
     5	# License agreement
     6	eula --agreed
     7	# System authorization information
     8	auth --enableshadow --passalgo=sha512
     9	# Use CDROM installation media
    10	cdrom
    11	# Run the Setup Agent on first boot
    12	firstboot --enable
    13	# Keyboard layouts
    14	keyboard --vckeymap=us --xlayouts='us'
    15	# System language
    16	lang en_US.UTF-8
    17	
    18	ignoredisk --only-use=sda
    19	# Network information
    20	network  --bootproto=dhcp --device=eno16777728 --onboot=off --ipv6=auto
    21	network  --bootproto=dhcp --hostname=linuxprobe.com
    22	# Root password
    23	rootpw --iscrypted $6$PeIDF1vfB3ZSSrXQ$fTg9v29sKCOGm48o1y/gqaTGhC62VIU1QuYYxxfyW3i4CqGgng3D.vYgKHhi8wiv/nmCQ0k4JWojFXzE28cV5/
    24	# System timezone
    25	timezone Antarctica/Casey --isUtc --nontp
    26	user --homedir=/home/linuxprobe --name=linuxprobe --password=$6$nSxR57Hi9jP3usBn$KgRL6Bz1zZxXus90/WmAENEqIfWfmOocBBVV.XgQ0H.lTXPfwrk/P7rOdROx2LqQJ2/j1LT9hooO2nCd.8iq1. --iscrypted --gecos="linuxprobe"
    27	# System bootloader configuration
    28	bootloader --location=mbr --boot-drive=sda
    29	autopart --type=lvm
    30	# Partition clearing information
    31	clearpart --none --initlabel 
    32	
    33	%packages
    34	@base
    35	@core
    36	@desktop-debugging
    37	@dial-up
    38	@fonts
    39	@gnome-desktop
    40	@guest-agents
    41	@guest-desktop-agents
    42	@input-methods
    43	@internet-browser
    44	@multimedia
    45	@print-client
    46	@x11
    47	
    48	%end
    49	
     1	#version=RHEL7
     2	# X Window System configuration information
     3	xconfig  --startxonboot
     4	
     5	# License agreement
     6	eula --agreed
     7	# System authorization information
     8	auth --enableshadow --passalgo=sha512
     9	# Use CDROM installation media
    10	cdrom
    11	# Run the Setup Agent on first boot
    12	firstboot --enable
    13	# Keyboard layouts
    14	keyboard --vckeymap=us --xlayouts='us'
    15	# System language
    16	lang en_US.UTF-8
    17	
    18	ignoredisk --only-use=sda
    19	# Network information
    20	network  --bootproto=dhcp --device=eno16777728 --onboot=off --ipv6=auto
    21	network  --bootproto=dhcp --hostname=linuxprobe.com
    22	# Root password
    23	rootpw --iscrypted $6$PeIDF1vfB3ZSSrXQ$fTg9v29sKCOGm48o1y/gqaTGhC62VIU1QuYYxxfyW3i4CqGgng3D.vYgKHhi8wiv/nmCQ0k4JWojFXzE28cV5/
    24	# System timezone
    25	timezone Antarctica/Casey --isUtc --nontp
    26	user --homedir=/home/linuxprobe --name=linuxprobe --password=$6$nSxR57Hi9jP3usBn$KgRL6Bz1zZxXus90/WmAENEqIfWfmOocBBVV.XgQ0H.lTXPfwrk/P7rOdROx2LqQJ2/j1LT9hooO2nCd.8iq1. --iscrypted --gecos="linuxprobe"
    27	# System bootloader configuration
    28	bootloader --location=mbr --boot-drive=sda
    29	autopart --type=lvm
    30	# Partition clearing information
    31	clearpart --none --initlabel 
    32	
    33	%packages
    34	@base
    35	@core
    36	@desktop-debugging
    37	@dial-up
    38	@fonts
    39	@gnome-desktop
    40	@guest-agents
    41	@guest-desktop-agents
    42	@input-methods
    43	@internet-browser
    44	@multimedia
    45	@print-client
    46	@x11
    47	
    48	%end
    49	

2.more

用于查看纯文本文件

格式:more[选项][文件]

代码语言:javascript复制
[root@linuxprobe ~]# more initial-setup-ks.cfg
#version=RHEL7
# X Window System configuration information
xconfig  --startxonboot

# License agreement
eula --agreed
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Run the Setup Agent on first boot
firstboot --enable
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8

ignoredisk --only-use=sda
# Network information
network  --bootproto=dhcp --device=eno16777728 --onboot=off --ipv6=auto
network  --bootproto=dhcp --hostname=linuxprobe.com
# Root password
rootpw --iscrypted $6$PeIDF1vfB3ZSSrXQ$fTg9v29sKCOGm48o1y/gqaTGhC62VIU1QuYYxxfyW
--More--(49%)

3.head

用于查看纯文本文档的当前N行

格式:head[选项][文件]

代码语言:javascript复制
[root@linuxprobe ~]# head -n 20 initial-setup-ks.cfg
#version=RHEL7
# X Window System configuration information
xconfig  --startxonboot

# License agreement
eula --agreed
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Run the Setup Agent on first boot
firstboot --enable
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8

ignoredisk --only-use=sda
# Network information
network  --bootproto=dhcp --device=eno16777728 --onboot=off --ipv6=auto

4.tail

用于查看纯文本文档后N行或持续刷新内容,

格式:tail[选项][文件]

代码语言:javascript复制
[root@linuxprobe ~]# tail -f /var/log/messages
Jun  7 17:33:35 linuxprobe systemd: Started Hostname Service.
Jun  7 17:36:18 linuxprobe dbus-daemon: dbus[1020]: [system] Activating via systemd: service name='net.reactivated.Fprint' unit='fprintd.service'
Jun  7 17:36:18 linuxprobe dbus[1020]: [system] Activating via systemd: service name='net.reactivated.Fprint' unit='fprintd.service'
Jun  7 17:36:18 linuxprobe systemd: Starting Fingerprint Authentication Daemon...
Jun  7 17:36:18 linuxprobe dbus-daemon: dbus[1020]: [system] Successfully activated service 'net.reactivated.Fprint'
Jun  7 17:36:18 linuxprobe dbus[1020]: [system] Successfully activated service 'net.reactivated.Fprint'
Jun  7 17:36:18 linuxprobe systemd: Started Fingerprint Authentication Daemon.
Jun  7 17:36:18 linuxprobe fprintd: ** (fprintd:8037): WARNING **: fprint init failed with error -99
Jun  7 17:36:18 linuxprobe systemd: fprintd.service: main process exited, code=exited, status=157/n/a
Jun  7 17:36:18 linuxprobe systemd: Unit fprintd.service entered failed state.

tai命令的强悍的功能是一个可以持续刷新一个文件的内容,当想要实现实时查看最新日志文件,tail -f

5.tr命令

用于替换文本文件中的字符

格式:tr[原始字符][目标字符]

很多时候我们想快速替换文本中的一些词汇,又或者把整个文本内容都进行替换,如果进行手工替换,难免工作了太大,尤其是需要处理大批量的内容时,进行手工替换更是不现实。这时,可以用cat命令读取待处理文本,然后通过管道符,把这些内容传递给tr命令进行替换操作

代码语言:javascript复制
[root@linuxprobe ~]# cat anaconda-ks.cfg| tr  [a-z] [A-Z]
#VERSION=RHEL7
# SYSTEM AUTHORIZATION INFORMATION
AUTH --ENABLESHADOW --PASSALGO=SHA512

# USE CDROM INSTALLATION MEDIA
CDROM
# RUN THE SETUP AGENT ON FIRST BOOT
FIRSTBOOT --ENABLE
IGNOREDISK --ONLY-USE=SDA
# KEYBOARD LAYOUTS
KEYBOARD --VCKEYMAP=US --XLAYOUTS='US'
# SYSTEM LANGUAGE
LANG EN_US.UTF-8

# NETWORK INFORMATION
NETWORK  --BOOTPROTO=DHCP --DEVICE=ENO16777728 --ONBOOT=OFF --IPV6=AUTO
NETWORK  --HOSTNAME=LINUXPROBE.COM
# ROOT PASSWORD
ROOTPW --ISCRYPTED $6$PEIDF1VFB3ZSSRXQ$FTG9V29SKCOGM48O1Y/GQATGHC62VIU1QUYYXXFYW3I4CQGGNG3D.VYGKHHI8WIV/NMCQ0K4JWOJFXZE28CV5/
# SYSTEM TIMEZONE
TIMEZONE ANTARCTICA/CASEY --ISUTC --NONTP
USER --HOMEDIR=/HOME/LINUXPROBE --NAME=LINUXPROBE --PASSWORD=$6$NSXR57HI9JP3USBN$KGRL6BZ1ZZXXUS90/WMAENEQIFWFMOOCBBVV.XGQ0H.LTXPFWRK/P7RODROX2LQQJ2/J1LT9HOOO2NCD.8IQ1. --ISCRYPTED --GECOS="LINUXPROBE"
# X WINDOW SYSTEM CONFIGURATION INFORMATION
XCONFIG  --STARTXONBOOT
# SYSTEM BOOTLOADER CONFIGURATION
BOOTLOADER --LOCATION=MBR --BOOT-DRIVE=SDA
AUTOPART --TYPE=LVM
# PARTITION CLEARING INFORMATION
CLEARPART --NONE --INITLABEL 

%PACKAGES
@BASE
@CORE
@DESKTOP-DEBUGGING
@DIAL-UP
@FONTS
@GNOME-DESKTOP
@GUEST-AGENTS
@GUEST-DESKTOP-AGENTS
@INPUT-METHODS
@INTERNET-BROWSER
@MULTIMEDIA
@PRINT-CLIENT
@X11

%END

6.wc

用于统计指定文本的行数、字数、字节数

格式:wc[参数]文本

-l 只显示行数

-w 只显示单词数

-c 只显示字节数

代码语言:javascript复制
[root@linuxprobe ~]# wc -l /etc/passwd
38 /etc/passwd

7.stat

用于产科文件的具体存储信息和时间等信息

格式:stat 文件名称

代码语言:javascript复制
[root@linuxprobe ~]# stat anaconda-ks.cfg
  File: ‘anaconda-ks.cfg’
  Size: 1241      	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 68912045    Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:admin_home_t:s0
Access: 2018-06-07 16:02:14.657903006  0800
Modify: 2018-05-25 03:53:59.660024224  0800
Change: 2018-05-25 03:53:59.660024224  0800
 Birth: -

8.cut

用于按“列“提取文本字符

格式:cut[参数]文本

在Linux中如何准确的提取最想要的数据,一般而言,按基于“行”的方式,来提取数据比较简单,只需要设置好要搜索的关键词即可。但是如何按列搜索,不仅要使用-f参数来设置需要看的列数,还需要使用-d参数来设置间隔符合。passwd在保存用户数据信息时,用户信息的每一项值之间是采用冒号来间隔

代码语言:javascript复制
[root@linuxprobe ~]# head -n 2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@linuxprobe ~]# cut -d: -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
dbus
polkitd
unbound
colord
usbmuxd
avahi
avahi-autoipd
libstoragemgmt
saslauth
qemu
rpc
rpcuser
nfsnobody
rtkit
radvd
ntp
chrony
abrt
pulse
gdm
gnome-initial-setup
postfix
sshd
tcpdump
linuxprobe

9.diff

用于比较多个文本文件的差异

格式:diff[参数][文件]

0 人点赞