1. 程式人生 > >文字檔案編輯命令——cat、more、head、tail、tr、wc、stat、cut、diff

文字檔案編輯命令——cat、more、head、tail、tr、wc、stat、cut、diff

1.cat命令

cat命令用於檢視純文字檔案(內容較少的),格式為“cat [選項] [檔案]”。

Linux系統中有多個用於檢視文字內容的命令,每個命令都有自己的特點,比如這個cat命令就是用於檢視內容較少的純文字檔案的。

如果在檢視文字內容時還想順便顯示行號的話,不妨在cat命令後面追加一個-n引數:

[[email protected] ~]# 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
………………省略部分輸出資訊………………

2.more命令

more命令用於檢視純文字檔案(內容較多的),格式為“more [選項]檔案”。

如果需要閱讀長篇小說或者非常長的配置檔案,因為一旦使用cat命令閱讀長篇的文字內容,資訊就會在螢幕上快速翻滾,導致自己還沒有來得及看到,內容就已經翻篇了。因此對於長篇的文字內容,推薦使用more命令來檢視。more命令會在最下面使用百分比的形式來提示您已經閱讀了多少內容。您還可以使用空格鍵或回車鍵向下翻頁:

[[email protected] ~]# 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

--More--(43%)

3.head命令

head命令用於檢視純文字文件的前N行,格式為“head [選項] [檔案]”。

在閱讀文字內容時,誰也難以保證會按照從頭到尾的順序往下看完整個檔案。如果只想檢視文字中前20行的內容,該怎麼辦呢?head命令可以派上用場了:

[[email protected] ~]# 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
[[email protected] ~]#

4.tail命令

tail命令用於檢視純文字文件的後N行或持續重新整理內容,格式為“tail [選項] [檔案]”。

我們可能還會遇到另外一種情況,比如需要檢視文字內容的最後20行,這時就需要用到tail命令了。tail命令的操作方法與head命令非常相似,只需要執行“tail -n 20 檔名”命令就可以達到這樣的效果。tail命令最強悍的功能是可以持續重新整理一個檔案的內容,當想要實時檢視最新日誌檔案時,這特別有用,此時的命令格式為“tail -f 檔名”:

[[email protected] ~]# tail -f /var/log/messages
May  4 07:56:38 localhost gnome-session: Window manager warning: Log level 16: 
STACK_OP_ADD: window 0x1e00001 already in stack
May  4 07:56:38 localhost gnome-session: Window manager warning: Log level 16: 
STACK_OP_ADD: window 0x1e00001 already in stack
May  4 07:56:38  localhost  vmusr[12982]: [ warning] [Gtk] gtk_disable_setlocale()
must be called before gtk_init()
May  4 07:56:50 localhost systemd-logind: Removed session c1.
Aug  1 01:05:31 localhost systemd: Time has been changed
Aug  1 01:05:31 localhost systemd: Started LSB: Bring up/down networking.
Aug  1 01:08:56 localhost dbus-daemon: dbus[1124]: [system] Activating service 
name='com.redhat.SubscriptionManager' (using servicehelper)
Aug  1 01:08:56 localhost dbus[1124]: [system] Activating service name='com.
redhat.SubscriptionManager' (using servicehelper)
Aug  1 01:08:57 localhost dbus-daemon: dbus[1124]: [system] Successfully activated
service 'com.redhat.SubscriptionManager'
Aug  1 01:08:57 localhost dbus[1124]: [system] Successfully activated service '
com.redhat.SubscriptionManager'

5.tr命令

tr命令用於替換文字檔案中的字元,格式為“tr [原始字元] [目標字元]”。

在很多時候,我們想要快速地替換文字中的一些詞彙,又或者把整個文字內容都進行替換,如果進行手工替換,難免工作量太大,尤其是需要處理大批量的內容時,進行手工替換更是不現實。這時,就可以先使用cat命令讀取待處理的文字,然後通過管道符把這些文字內容傳遞給tr命令進行替換操作即可。例如,把某個文字內容中的英文全部替換為大寫:

[[email protected] ~]# 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=LOCALHOST.LOCALDOMAIN
# ROOT PASSWORD
ROOTPW --ISCRYPTED $6$PDJJF42G8C6PL069$II.PX/YFAQPO0ENW2PA7MOMKJLYOAE2ZJMZ2UZJ7
BH3UO4OWTR1.WK/HXZ3XIGMZGJPCS/MGPYSSOI8HPCT8B/
# SYSTEM TIMEZONE
TIMEZONE AMERICA/NEW_YORK --ISUTC
USER --NAME=LINUXPROBE --PASSWORD=$6$A9V3INSTNBWEIR7D$JEGFYWBCDOOOKJ9SODECCDO.
ZLF4OSH2AZ2SS2R05B6LZ2A0V2K.RJWSBALL2FEKQVGF640OA/TOK6J.7GUTO/ --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命令用於統計指定文字的行數、字數、位元組數,格式為“wc [引數] 文字”。

wc的引數以及相應的作用如表所示。

                                                                  wc的引數以及作用

引數

作用

-l

只顯示行數

-w

只顯示單詞數

-c

只顯示位元組數

Linux系統中,passwd是用於儲存系統賬戶資訊的檔案,要統計當前系統中有多少個使用者,可以使用下面的命令來進行查詢,是不是很神奇:

[[email protected] ~]# wc -l /etc/passwd
38 /etc/passwd

7.stat命令

stat命令用於檢視檔案的具體儲存資訊和時間等資訊,格式為“stat 檔名稱”。

stat命令可以用於檢視檔案的儲存資訊和時間等資訊,命令stat anaconda-ks.cfg會顯示出檔案的三種時間狀態(已加粗):Access、Modify、Change。這三種時間的區別將在下面的touch命令中詳細詳解:

[[email protected] ~]# stat anaconda-ks.cfg
File: ‘anaconda-ks.cfg’
Size: 1213 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 68912908 Links: 1
Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root)
Context: system_u:object_r:admin_home_t:s0
Access: 2017-07-14 01:46:18.721255659 -0400
Modify: 2017-05-04 15:44:36.916027026 -0400
Change: 2017-05-04 15:44:36.916027026 -0400
Birth: -

8.cut命令

cut命令用於按“列”提取文字字元,格式為“cut [引數] 文字”。

在Linux系統中,如何準確地提取出最想要的資料,這也是我們應該重點學習的內容。一般而言,按基於“行”的方式來提取資料是比較簡單的,只需要設定好要搜尋的關鍵詞即可。但是如果按列搜尋,不僅要使用-f引數來設定需要看的列數,還需要使用-d引數來設定間隔符號。passwd在儲存使用者資料資訊時,使用者資訊的每一項值之間是採用冒號來間隔的,接下來我們使用下述命令嘗試提取出passwd檔案中的使用者名稱資訊,即提取以冒號(:)為間隔符號的第一列內容:

[[email protected] ~]# head -n 2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[[email protected] ~]# 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命令用於比較多個文字檔案的差異,格式為“diff [引數] 檔案”。

在使用diff命令時,不僅可以使用--brief引數來確認兩個檔案是否不同,還可以使用-c引數來詳細比較出多個檔案的差異之處,這絕對是判斷檔案是否被篡改的有力神器。例如,先使用cat命令分別檢視diff_A.txt和diff_B.txt檔案的內容,然後進行比較:

[[email protected] ~]# cat diff_A.txt
Welcome to linuxprobe.com
Red Hat certified
Free Linux Lessons
Professional guidance
Linux Course
[[email protected] ~]# cat diff_B.txt
Welcome tooo linuxprobe.com
Red Hat certified
Free Linux LeSSonS
////////.....////////
Professional guidance
Linux Course
接下來使用diff--brief命令顯示比較後的結果,判斷檔案是否相同:
[[email protected] ~]# diff --brief diff_A.txt diff_B.txt
Files diff_A.txt and diff_B.txt differ
最後使用帶有-c引數的diff命令來描述檔案內容具體的不同:
[[email protected] ~]# diff -c diff_A.txt diff_B.txt
*** diff_A.txt 2017-08-30 18:07:45.230864626 +0800
--- diff_B.txt 2017-08-30 18:08:52.203860389 +0800
***************
*** 1,5 ****
! Welcome to linuxprobe.com
Red Hat certified
! Free Linux Lessons
Professional guidance
Linux Course
--- 1,7 ----
! Welcome tooo linuxprobe.com
!
Red Hat certified
! Free Linux LeSSonS
! ////////.....////////
Professional guidance
Linux Course