溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Linux文件內容相關命令怎么使用

發(fā)布時間:2023-04-25 10:01:26 來源:億速云 閱讀:114 作者:iii 欄目:開發(fā)技術

本篇內容主要講解“Linux文件內容相關命令怎么使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Linux文件內容相關命令怎么使用”吧!

cat 合并文件或查看文件內容

1、簡介

cat 是concatenate 單詞的縮寫,或者理解成貓,"喵"一下文件內容,即顯示文件內容。

可以用來顯示單個文件內容 可以將幾個文件連接起來一起顯示 從標準輸入中讀取內容并顯示,與重定向或追加符號配合使用 功能 舉個栗子 查看文件內容 cat xiezhr.txt 查看xiezhr.txt 的內容 把多個文件合并成一個 cat xiezhr1.txt xiezhr2.txt >newxiezhr.txt 編輯或追加內容到文件尾部 cat >>xiezhr.txt<<EOF My blog is www.xiezhrspace.cn. EOF 清空文件內容 cat /dev/null >xiezhr.txt 命令可以把xiezhr.txt文件內容清空,但是文件還是存在的

2、語法格式

cat [參數(shù)選項] [文件]

3、參數(shù)說明

參數(shù) 參數(shù)說明 -n 從1開始對所有輸出的內容按行編號 -b 和-n功能類似,但會忽略顯示空白行行號 -s 當文件內容中包含多個空白行時,為了閱讀更加方便,-s可以將多個空白行替換為一個空白行 -E 每一行的行尾顯示$符號 -T 將Tab(制表符)字符顯示為^I

4、實踐操作

① 不加參數(shù),直接執(zhí)行cat 查看文件內容

[root@xiezhr test]# cat xiezhr.txt 
個人公眾號:XiezhrSpace


個人博客:www.xiezhrspace.cn



個人微信號:xie_zhr

歡迎您的關注!

② 通過非交互式創(chuàng)建編輯xiezhr.txt 文件
上面的xiezhr.txt文件我們可以通過之前說過的touch xiezhr.txt先創(chuàng)建一個空白文件,
然后通過vi/vim(下一期我們會具體說一說它的用法)編輯xiezhr.txt的內容。

當然了,這個是我們之前的玩法,今天我們嘗試著以一種新的方式創(chuàng)建xiezhr.txt文件并編輯其內容。

[root@xiezhr test]# ls
a.txt  c.txt  dir  dir2  dir3  dir5  dir6  dir_bak  movie.tar.gz  test.txt  tt.txt
[root@xiezhr test]# cat >xiezhr.txt<<EOF
> 個人公眾號:XiezhrSpace
> 
> 
> 個人博客:www.xiezhrspace.cn
> 
> 
> 
> 個人微信號:xie_zhr
> 
> 歡迎您的關注!
> EOF  #注意,這里需要按回車結束,EOF為標簽,我們可以用其他標簽替代,但是必須成對出現(xiàn)。
[root@xiezhr test]# ls
a.txt  c.txt  dir  dir2  dir3  dir5  dir6  dir_bak  movie.tar.gz  test.txt  tt.txt  xiezhr.txt

③ 分別用【-n】【-b】顯示上面創(chuàng)建的文件前的序號

[root@xiezhr test]# cat -n xiezhr.txt 
     1  個人公眾號:XiezhrSpace
     2
     3
     4  個人博客:www.xiezhrspace.cn
     5
     6
     7
     8  個人微信號:xie_zhr
     9
    10  歡迎您的關注!
[root@xiezhr test]# cat -b xiezhr.txt 
     1  個人公眾號:XiezhrSpace


     2  個人博客:www.xiezhrspace.cn



     3  個人微信號:xie_zhr

     4  歡迎您的關注!

④ 使用【-E】參數(shù),在每一行末尾顯示$

[root@xiezhr test]# cat -E xiezhr.txt 
個人公眾號:XiezhrSpace$
$
$
個人博客:www.xiezhrspace.cn$
$
$
$
個人微信號:xie_zhr$
$
歡迎您的關注!$

⑤使用 【-s】參數(shù),把文件內容中的多個空白行變成一個空白行,方便我們閱讀

[root@xiezhr test]# cat -s xiezhr.txt 
個人公眾號:XiezhrSpace

個人博客:www.xiezhrspace.cn

個人微信號:xie_zhr

歡迎您的關注!

上面文件中的多個空白行就變成了一個,這樣當文件內容比較多的時候,閱讀起來就比較友好了。

當然也可以將所有的空白行都去掉,這個我們會在后面的命令中說

⑥ 連接多個文件并顯示內容

# 創(chuàng)建test1.txt 文件
[root@xiezhr test]# cat >test1.txt <<EOF
> 這個是test1.txt的內容
> EOF
# 創(chuàng)建test2.txt文件
[root@xiezhr test]# cat >test2.txt <<EOF           
> 這個是test2.txt的內容
> EOF
# 將test1 test2 文件合并并輸出
[root@xiezhr test]# cat test1.txt test2.txt test3.txt
這個是test1.txt的內容
這個是test2.txt的內容

more 分頁顯示文件內容

1、簡介

more 其功能和cat 類似,cat 是將整個文件內容一次顯示出來,而more一頁一頁的顯示文件內容。我們可以根據(jù)其中文意思更多去理解,除了這頁,還有更多頁內容,這樣是不是就記住了呢。????

2、語法格式

more [參數(shù)選項] [文件]

3、參數(shù)說明

參數(shù)參數(shù)說明
-num指定屏幕顯示大小為num行
+num從行號num開始顯示

4、實踐操作

使用more命令打開文本之后,會進入vi交互界面。這時候我們就可以使用vi編輯器功能。那么都有些說明功能呢?

具體看下表總結

子命令說明
h或?查看幫助
空格鍵[space]向下滾動一屏
b返回上一屏
Enter向下顯示一行
/要查找的內容查找指定的文本內容
=輸出當前行的行號
:f輸出打開的文件名和行號
v調用vi編輯器
!調用shell并執(zhí)行命令
q退出more

① 不帶參數(shù)查看文件

[root@xiezhr ~]# more /etc/services 
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10
#
......由于文件內容比較多此處省略n行.....
ssh             22/udp                          # The Secure Shell (SSH) Protocol
telnet          23/tcp
--More--(0%)

②使用【-num】參數(shù)每頁顯示num行

[root@xiezhr ~]# more -5 /etc/services 
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10

② 使用【+num】參數(shù),從第num行開始顯示

[root@xiezhr ~]# more +666 -6
Usage: more [options] file...

Options:
  -d        display help instead of ring bell
  -f        count logical, rather than screen lines
  -l        suppress pause after form feed
  -p        do not scroll, clean screen and display text
  -c        do not scroll, display text and clean line ends
  -u        suppress underlining
  -s        squeeze multiple blank lines into one
  -NUM      specify the number of lines per screenful
  +NUM      display file beginning from line number NUM
  +/STRING  display file beginning from search string match
  -V        output version information and exit

less 分頁顯示文件內容

1、簡介

lss 功能類似于more 它的中文意思是少,看似是功能更少了,其實不然。它是more的增強版,具有更多功能

2、語法格式

less [參數(shù)選項] [文件]

3、參數(shù)說明

參數(shù)參數(shù)說明
-N顯示每行的行號
-m顯示進度百分比
-s當出現(xiàn)連續(xù)空行時壓縮為一行顯示
-e文件顯示到最后時自動退出,若不適用參數(shù)需要用快捷鍵q退出less

4、實踐操作

使用less命令打開文本之后,會進入vi交互界面。下面列出了一些常用交互式子命令

子命令說明
空格鍵[space]向后翻一頁
b向前翻一頁
回車鍵[Enter]向下滾動一行
&uarr;向上滾動一行
&darr;向下滾動一行
[PgUp]向前翻一頁
[PgDn]向后翻一頁
/字符串向下搜素“字符串”
?字符串向上搜素”字符串“
n向后查找下一個匹配的文本
N向上查找上一個匹配的文本
v進入vi編輯界面
!調用shell,并執(zhí)行命令
G移動到最后一行
g移動到第一行
h顯示幫助界面
q退出less

① 不帶參數(shù)查看文件

[root@xiezhr ~]# less /etc/services 
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10
......省略n行......

② 帶上【-N】參數(shù),顯示行號

[root@xiezhr ~]# less -N /etc/services 
      1 # /etc/services:
      2 # $Id: services,v 1.55 2013/04/14 ovasik Exp $
      3 #
      4 # Network services, Internet style
      5 # IANA services version: last updated 2013-04-10
      6 #
      7 # Note that it is presently the policy of IANA to assign a single well-known
......此處省略n行......

head 顯示文件內容頭部

1、簡介

head 中文意思時頭部,相信你也想到了,head的功能就是顯示文件內容的頭部。默認顯示頭10行

2、語法格式

head [參數(shù)選項] [文件]

3、參數(shù)說明

參數(shù)選項參數(shù)說明
-n顯示前n行

4、實踐操作

① 不帶參數(shù),默認顯示前十行

[root@xiezhr ~]# head /etc/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

②帶上【-n】參數(shù),顯示文件前n行

[root@xiezhr ~]# head -n 5 /etc/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

tail 顯示文件內容尾部

1、簡介

tail 中文意思時尾巴,到這里你應該知道這個命令時干嘛用的了。你想的沒錯,就是顯示文件最后10行,和命令head相反

2、語法格式

tail [參數(shù)選項] [文件]

3、參數(shù)說明

參數(shù)參數(shù)說明
-n指定顯示的行數(shù)
-f實時輸出文件變化后追加的數(shù)據(jù)

4、實踐操作

① 不帶參數(shù),默認顯示最后10行

[root@xiezhr ~]# tail /etc/passwd
abrt:x:173:173::/etc/abrt:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
syslog:x:996:994::/home/syslog:/bin/false
git:x:995:993:git version control:/home/git:/bin/bash
nginx:x:994:992:Nginx web server:/var/lib/nginx:/sbin/nologin
mysql:x:1000:1000::/home/mysql:/bin/bash
xiezhr:x:1001:1001::/home/xiezhr:/bin/bash

② 顯示文件尾部5行

[root@xiezhr ~]# tail -n 5 /etc/passwd
syslog:x:996:994::/home/syslog:/bin/false
git:x:995:993:git version control:/home/git:/bin/bash
nginx:x:994:992:Nginx web server:/var/lib/nginx:/sbin/nologin
mysql:x:1000:1000::/home/mysql:/bin/bash
xiezhr:x:1001:1001::/home/xiezhr:/bin/bash

③ 實時跟蹤日志的變化(實際工作中經(jīng)常用到,需要牢靠掌握)

[root@xiezhr /]# tail -f /var/log/nginx/access.log
118.126.124.141 - - [22/Mar/2022:23:11:10 +0800] "GET /categories/%E8%A7%89%E9%86%92%E5%B9%B4%E4%BB%A3/ HTTP/1.1" 200 10085 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36" "-"
14.215.156.21 - - [22/Mar/2022:23:11:10 +0800] "GET /medias/logo.png HTTP/1.1" 200 112674 "-" "-" "-"
54.36.148.108 - - [22/Mar/2022:23:12:14 +0800] "GET / HTTP/1.1" 200 14641 "-" "Mozilla/5.0 (compatible; AhrefsBot/7.0; +http://ahrefs.com/robot/)" "-"
69.162.124.234 - - [22/Mar/2022:23:13:23 +0800] "HEAD / HTTP/1.1" 200 0 "https://www.xiezhrspace.cn" "Mozilla/5.0+(compatible; UptimeRobot/2.0; http://www.uptimerobot.com/)" "-"
216.245.221.91 - - [22/Mar/2022:23:14:10 +0800] "HEAD / HTTP/1.1" 200 0 "https://www.xiezhrspace.cn" "Mozilla/5.0+(compatible; UptimeRobot/2.0; http://www.uptimerobot.com/)" "-"
207.46.13.67 - - [22/Mar/2022:23:16:44 +0800] "GET /archives/f454bf8f.html HTTP/2.0" 200 15842 "-" "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)" "-"
40.77.167.21 - - [22/Mar/2022:23:17:00 +0800] "GET /libs/share/js/social-share.min.js HTTP/2.0" 200 9195 "-" "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)" "-"
157.55.39.183 - - [22/Mar/2022:23:17:00 +0800] "GET /libs/share/css/share.min.css HTTP/2.0" 200 1082 "-" "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)" "-"
69.162.124.234 - - [22/Mar/2022:23:18:23 +0800] "HEAD / HTTP/1.1" 200 0 "https://www.xiezhrspace.cn" "Mozilla/5.0+(compatible; UptimeRobot/2.0; http://www.uptimerobot.com/)" "-"
216.245.221.91 - - [22/Mar/2022:23:19:10 +0800] "HEAD / HTTP/1.1" 200 0 "https://www.xiezhrspace.cn" "Mozilla/5.0+(compatible; UptimeRobot/2.0; http://www.uptimerobot.com/)" "-"

tailf 跟蹤日志文件

1、簡介

tailf 功能與 tail -f 命令基本相同,記住一個即可

diff 比較兩個文件的不同

1、簡介

diff 命令用于逐行比較兩個文件的不同,并輸出差異內容

2、語法格式

diff [參數(shù)選項] [文件1] [文件2]

3、參數(shù)說明

參數(shù)參數(shù)說明
-y以并列的方式顯示兩個文件的不同
-W在使用-y參數(shù)時,指定顯示的寬度
-c使用上下文的輸出格式
-u使用統(tǒng)一格式輸出

4、實踐操作

① 不帶參數(shù),比較兩個文件

[root@xiezhr test]# cat test1.txt 
1
2
3
4
[root@xiezhr test]# cat test2.txt 
3
4
5
[root@xiezhr test]# diff test1.txt test2.txt 
1,2d0
< 1
< 2
4a3
> 5

以上結果說明

a 代表add 新加 c 代表change 改變 d 代表delete 刪除 <代表第一個文件>代表第二個文件

② 帶【-y -w】參數(shù)比較兩個文件

[root@xiezhr test]# diff -y test1.txt test2.txt 
1                                                             <
2                                                             <
3                                                               3
4                                                               4
                                                              > 5

③ 使用【-c】比較兩個文件 (個人推薦使用這個,結果顯示直觀,一看就懂)

[root@xiezhr test]# diff -c test1.txt test2.txt 
*** test1.txt   2022-03-26 10:04:38.400526014 +0800
--- test2.txt   2022-03-26 10:05:50.597539120 +0800
***************
*** 1,4 ****
- 1
- 2
  3
  4
--- 1,3 ----
  3
  4
+ 5

結果說明:

'+' 表示test2 比test1 多的 '_' 表示test2 比test1 少的

④ 使用【-u】比較兩個文件

[root@xiezhr test]# diff -u test1.txt test2.txt 
--- test1.txt   2022-03-26 10:04:38.400526014 +0800
+++ test2.txt   2022-03-26 10:05:50.597539120 +0800
@@ -1,4 +1,3 @@
-1
-2
 3
 4
+5

vimdiff 可視化比較工具

1、簡介

vimdiff 命令我們可以拆分為vim和diff 由此可知,該命令是調用vim打開文件來可視化分屏比較多個文件

2、語法格式

vimdiff [參數(shù)選項] [文件1] [文件2] [文件3]

3、參數(shù)說明

一般不加參數(shù)

4、實踐操作

比較多個文件

[root@xiezhr test]# vimdiff test1.txt test2.txt test3.txt

Linux文件內容相關命令怎么使用

退出vimdiff 需要執(zhí)行兩次退出vim的操作(:q)

wc 統(tǒng)計文件的的行數(shù)、單詞數(shù)或字節(jié)數(shù)

1、簡介

wc 命令可以理解為是words count 的縮寫,說到縮寫,我知道你想到了“廁所”,可真不是廁所的縮寫。

用于統(tǒng)計文件的行數(shù)、單詞數(shù)或字節(jié)數(shù)

2、語法格式

wc [參數(shù)選項] [文件]

3、參數(shù)說明

參數(shù)參數(shù)說明
-c統(tǒng)計字節(jié)數(shù)
-w統(tǒng)計單詞數(shù)
-l統(tǒng)計行數(shù)
-L打印最長行的長度
-m統(tǒng)計字符數(shù)

4、實踐操作

① 不帶參數(shù)

[root@xiezhr test]# cat xiezhr.txt 
個人公眾號:XiezhrSpace


個人博客:www.xiezhrspace.cn



個人微信號:xie_zhr

歡迎您的關注!
[root@xiezhr test]# wc xiezhr.txt 
 10   4 118 xiezhr.txt

結果說明,不帶參數(shù),直接輸出的結果四10行,4個單詞(按照英文單詞方式記的????),118個字節(jié)

② 帶上參數(shù),查看文件的字數(shù)、字節(jié)數(shù)、字符數(shù)

[root@xiezhr test]# cat xiezhr.txt 
個人公眾號:XiezhrSpace


個人博客:www.xiezhrspace.cn



個人微信號:xie_zhr

歡迎您的關注!
[root@xiezhr test]# wc -c xiezhr.txt 
118 xiezhr.txt
[root@xiezhr test]# wc -l xiezhr.txt 
10 xiezhr.txt
[root@xiezhr test]# wc -m xiezhr.txt 
70 xiezhr.txt
[root@xiezhr test]# wc -w xiezhr.txt 
4 xiezhr.txt
[root@xiezhr test]# wc -L xiezhr.txt 
28 xiezhr.txt

③ 查看登錄系統(tǒng)的用戶數(shù)

# 查看哪些用戶登錄系統(tǒng)
[root@xiezhr test]# who
root     pts/0        2022-03-26 10:03 (39.130.60.84)
root     pts/1        2022-03-26 10:36 (39.130.60.84)
root     pts/2        2022-03-26 10:56 (39.130.60.84)
# 查看一共有多少用戶登錄系統(tǒng)
[root@xiezhr test]# who |wc -l
3

sort 文本排序

1、簡介

sort 中文意思就是排序,所以呢該命令用于將輸入的文件內容按照指定規(guī)則排序

2、語法格式

sort [參數(shù)選項] [文件]

3、參數(shù)說明

參數(shù)參數(shù)選項
-b忽略每行開頭存在的空格字符
-n按照數(shù)值的大小進行排序
-r倒敘排序
-u去除重復行
-t指定分隔符
-k按指定區(qū)間排序

4、實踐操作

默認是從首字符向后,依次按照ASCII碼升序排列

①不帶參數(shù)進行排序

[root@xiezhr test]# cat a.txt 

192.168.205.23
192.168.205.23
192.168.205.23
192.168.205.24
192.168.205.21
192.168.205.24

[root@xiezhr test]# sort a.txt 


192.168.205.21
192.168.205.23
192.168.205.23
192.168.205.23
192.168.205.24
192.168.205.24

② 使用【-n】參數(shù),按數(shù)字從小到大排序

[root@xiezhr test]# sort -n a.txt 


192.168.205.21
192.168.205.23
192.168.205.23
192.168.205.23
192.168.205.24
192.168.205.24

③ 使用【-r】參數(shù),按降序排序

[root@xiezhr test]# sort -nr a.txt 
192.168.205.24
192.168.205.24
192.168.205.23
192.168.205.23
192.168.205.23
192.168.205.21

④使用【-u】參數(shù),去除重復

[root@xiezhr test]# sort -u a.txt 
192.168.205.21
192.168.205.23
192.168.205.24

⑤使用【-t -k】按指定列排序

[root@xiezhr test]# cat a2.txt 
小謝 18歲
小明 17歲
小林 25歲
曉燕 30歲
小李 17歲
[root@xiezhr test]# sort a2.txt 
小明 17歲
小李 17歲
小林 25歲
小謝 18歲
曉燕 30歲
# -t 后面指定以空格為分隔符, -k 后面參數(shù)表示按第二列排序
[root@xiezhr test]# sort -t " " -k2 a2.txt 
小明 17歲
小李 17歲
小謝 18歲
小林 25歲
曉燕 30歲

uniq 去除重復行

1、簡介

uniq 命令用于檢查及刪除文本文件中重復出現(xiàn)的行列,一般與 sort 命令結合使用。

2、語法格式

uniq [參數(shù)選項] [文件或標準輸出]

3、參數(shù)說明

參數(shù)參數(shù)說明
-c去除重復行,并計算每行出現(xiàn)的次數(shù)
-d只顯示重復的行
-u只顯示唯一的行

4、實踐操作

① 不帶參數(shù)去重

[root@xiezhr test]# cat test1.txt 
個人公眾號:XiezhrSpace
個人公眾號:XiezhrSpace
個人公眾號:XiezhrSpace
個人博客:www.xiezhrspace.cn
個人博客:www.xiezhrspace.cn
[root@xiezhr test]# uniq test1.txt 
個人公眾號:XiezhrSpace
個人博客:www.xiezhrspace.cn

②加上【-c】參數(shù),顯示相應出現(xiàn)的次數(shù)

[root@xiezhr test]# cat test1.txt 
個人公眾號:XiezhrSpace
個人公眾號:XiezhrSpace
個人公眾號:XiezhrSpace
個人博客:www.xiezhrspace.cn
個人博客:www.xiezhrspace.cn
[root@xiezhr test]# uniq -c test1.txt 
      3 個人公眾號:XiezhrSpace
      2 個人博客:www.xiezhrspace.cn

③ 配合著sort命令一起使用

[root@xiezhr test]# cat test1.txt 
個人公眾號:XiezhrSpace
個人公眾號:XiezhrSpace
個人博客:www.xiezhrspace.cn
個人公眾號:XiezhrSpace
個人博客:www.xiezhrspace.cn
個人博客:www.xiezhrspace.cn
[root@xiezhr test]# uniq test1.txt 
個人公眾號:XiezhrSpace
個人博客:www.xiezhrspace.cn
個人公眾號:XiezhrSpace
個人博客:www.xiezhrspace.cn
[root@xiezhr test]# sort -n test1.txt | uniq -c
      3 個人公眾號:XiezhrSpace
      3 個人博客:www.xiezhrspace.cn

join 按兩個文件的相同字段合并

1、簡介

man 命令用于將兩個文件中,指定欄位內容相同的行連接起來

2、語法格式

join [參數(shù)選項] [文件1] [文件2]

3、參數(shù)說明

參數(shù)參數(shù)選項
-1以第一個文件的指定字段為基礎進行合并
-2以第二個文件的指定字段為基礎進行合并
-i比較字段時忽略大小寫
-a<1或2>除了顯示原來的輸出內容之外,還顯示指令文件中沒有相同欄位的行

4、實踐操作

合并文本

[root@xiezhr test]# cat a1.txt 
小謝 男
小明 女
小林 男
曉燕 女
[root@xiezhr test]# cat a2.txt 
小謝 18歲
小明 17歲
小林 25歲
曉燕 30歲
[root@xiezhr test]# join a1.txt a2.txt 
小謝 男 18歲
小明 女 17歲
小林 男 25歲
曉燕 女 30歲

paste 合并文件

1、簡介

paste 命令能將文件按照行與行進行合并,中間使用tab隔開

2、語法格式

paste [參數(shù)選項] [文件]

3、參數(shù)說明

參數(shù)參數(shù)說明
-d指定合并的分隔符,默認是tab
-s每個文件占用一行

4、實踐操作

①不帶參數(shù)合并文件

[root@xiezhr test]# cat a1.txt 
小謝 男
小明 女
小林 男
曉燕 女
[root@xiezhr test]# cat a2.txt 
小謝 18歲
小明 17歲
小林 25歲
曉燕 30歲
小李 17歲
[root@xiezhr test]# paste a1.txt a2.txt 
小謝 男 小謝 18歲
小明 女 小明 17歲
小林 男 小林 25歲
曉燕 女 曉燕 30歲
        小李 17歲

②通過【-d】參數(shù),指定分隔符

[root@xiezhr test]# cat a1.txt 
小謝 男
小明 女
小林 男
曉燕 女
[root@xiezhr test]# cat a2.txt 
小謝 18歲
小明 17歲
小林 25歲
曉燕 30歲
小李 17歲
[root@xiezhr test]# paste -d: a1.txt a2.txt 
小謝 男:小謝 18歲
小明 女:小明 17歲
小林 男:小林 25歲
曉燕 女:曉燕 30歲
:小李 17歲

③使用【-s】參數(shù),合并內容

[root@xiezhr test]# cat a1.txt 
小謝 男
小明 女
小林 男
曉燕 女
[root@xiezhr test]# paste -s a1.txt 
小謝 男 小明 女 小林 男 曉燕 女

split 分割文件

1、簡介

split 是分割的意思,按照指定行數(shù)或者指定文件大小分割文件,將其分割成多個文件。

2、語法格式

split [參數(shù)選項] [輸入文件] [輸入文件名前綴]

3、參數(shù)說明

參數(shù)參數(shù)說明
-l指定分割后文件的最大行數(shù)

4、實踐操作

按行數(shù)進行分割

[root@xiezhr test]# cat a.txt 

192.168.205.23
192.168.205.23
192.168.205.23
192.168.205.24
192.168.205.21
192.168.205.24
#原來的大文件"a.txt"切割成多個以"x"開頭的小文件。而在這些小文件中,每個文件都只有3行內容。
[root@xiezhr test]# split -3 a.txt 
[root@xiezhr test]# ls
a1.txt  a2.txt  a.txt  c.txt  dir  dir2  dir3  dir5  dir6  dir_bak  movie.tar.gz  m.tx  test1.txt  test2.txt  test3.txt  test.txt  tt.txt  xaa  xab  xac  xiezhr.txt

cut 從文本中提取一段文字并輸出

1、簡介

cut 意思是剪切,所以其功能就是把文件每一行剪切一段文字,并將文字輸出

2、語法格式

cut [參數(shù)選項] [文件]

3、參數(shù)說明

參數(shù)參數(shù)選項
-c以字符為單位進行分割
-d自定義分割符,默認以tab為分割符
-f與選項-d一起使用,指定顯示哪個區(qū)域
N第N個字節(jié)、字符或字段
N-從第N個字符、字節(jié)或字段開始直至行尾
N-M從第N到第M(包含M)個字節(jié)、字符或字段
-M從第1到第M(包含M)個字節(jié)、字符或字段

4、實踐操作

以字符為單位進行剪切

[root@xiezhr dir]# cat a.txt 
I am xiezhr.I love coding.

[root@xiezhr dir]# cut -c 4 a.txt  #輸出第4個字符
m

[root@xiezhr dir]# cut -c 3-4 a.txt #輸出第3到第4個字符
am

[root@xiezhr dir]# cut -c -4 a.txt  #輸出第1到第4個字符
I am

[root@xiezhr dir]# cut -c 4- a.txt  #輸出第4個到最后個字符
m xiezhr.I love coding.

tr 替換或刪除字符

1、簡介

tr 命令用于替換或刪除文件中的字符。

2、語法格式

tr [參數(shù)選項] [字符1] [字符2]

3、參數(shù)說明

參數(shù)參數(shù)說明
-d刪除字符
-s保留連續(xù)字符的第一個字符,刪去其他字符
-c反選設定字符。也就是符合 SET1 的部份不做處理,不符合的剩余部份才進行轉換

4、實踐操作

①將文件中的&lsquo;is&rsquo; 替換為&lsquo;ab&rsquo; 注意:凡是文中出現(xiàn)的"i"都替換成“a”,"s"均被替換成“b”,而不僅僅是“is”替換為字符串“ab”

[root@xiezhr dir]# clear
[root@xiezhr dir]# cat a.txt 
I am xiezhr.
I love coding. 
My official account is XiezhrSpace.
My blog is www.xiezhrspace.cn.
My QQ is 1666397814.

Welcome to follow me!

We study together and make progress together.

[root@xiezhr dir]# tr 'is' 'ab' < a.txt 
I am xaezhr.
I love codang. 
My offacaal account ab XaezhrSpace.
My blog ab www.xaezhrbpace.cn.
My QQ ab 1666397814.

Welcome to follow me!

We btudy together and make progrebb together.

②大小寫替換

[root@xiezhr dir]# tr '[a-z]' '[A-Z]' < a.txt 
I AM XIEZHR.
I LOVE CODING. 
MY OFFICIAL ACCOUNT IS XIEZHRSPACE.
MY BLOG IS WWW.XIEZHRSPACE.CN.
MY QQ IS 1666397814.

WELCOME TO FOLLOW ME!

WE STUDY TOGETHER AND MAKE PROGRESS TOGETHER.

③ 將數(shù)字0-9 替換成a-j

[root@xiezhr dir]# tr '[0-9]' '[a-j]' < a.txt 
I am xiezhr.
I love coding. 
My official account is XiezhrSpace.
My blog is www.xiezhrspace.cn.
My QQ is bgggdjhibe.

Welcome to follow me!

We study together and make progress together.

④刪除文中的字符(注:&rsquo;M&lsquo;,'y'每個字符都會被刪除,而不僅僅是“My”字符被刪除)

[root@xiezhr dir]# tr -d 'My' < a.txt 
I am xiezhr.
I love coding. 
 official account is XiezhrSpace.
 blog is www.xiezhrspace.cn.
 QQ is 1666397814.

Welcome to follow me!

We stud together and make progress together.

⑤ 刪除文件中換行符“\n”,制表符“\t”

[root@xiezhr dir]# tr -d '\n\t' < a.txt 
I am xiezhr.I love coding. My official account is XiezhrSpace.My blog is www.xiezhrspace.cn.My QQ is 1666397814.Welcome to follow me!We study together and make progress together.[root@xiezhr dir]#

⑥使用【-c】參數(shù)取反

# 下面命令將不是‘0-9' 的數(shù)字替換為‘*'
[root@xiezhr dir]# tr -c '0-9' '*' < a.txt 
*********************************************************************************************************1666397814*************************************************************************[root@xiezhr dir]#

Linux系統(tǒng)中可以使用多種命令來處理文件內容。cat命令將文件的全部內容輸出到控制臺,但它不能上下滾動瀏覽;more命令可以進行分頁顯示,但只能向下滾動,若要向上則需重新輸入命令;less命令集合了cat和more的優(yōu)點,不僅可以翻頁,還支持前后搜索;head和tail命令可以輸出文件的頭幾行或尾幾行;grep和find是用于搜索所需信息的強大命令,在數(shù)據(jù)挖掘和日志分析的應用場景中得到廣泛應用。這些命令都有自己特別的用途,理解和掌握它們可以使我們在Linux系統(tǒng)中更加高效地工作和處理文件內容。

到此,相信大家對“Linux文件內容相關命令怎么使用”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI