溫馨提示×

溫馨提示×

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

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

測試過程中常用的linux命令之【查看文件指定行的內(nèi)容】

發(fā)布時間:2020-07-30 01:43:33 來源:網(wǎng)絡(luò) 閱讀:1356 作者:ilanqing 欄目:軟件技術(shù)

    在開展測試工作的過程中,通常要接觸到服務(wù)器,對于linux服務(wù)器,總結(jié)一些常用的命令。


  •     準(zhǔn)備工作

    為了能直觀展示命令結(jié)果,使用腳本創(chuàng)建一個文件,在顯示文件內(nèi)容的同時,也直觀的顯示行號。

#!/bin/bash

FileName=TestFile.log
touch ./$FileName

i=1
while [ $i -le $1 ]
do
        echo "the line number is $i" >> $FileName
        let "i=$i+1"
done


命令選項示例
head-n,顯示行數(shù)
[root@Durian scripts]# head -n 5 TestFile.log 
the line number is 1
the line number is 2
the line number is 3
the line number is 4
the line number is 5

#顯示文件的前5行

[root@Durian scripts]# head -n -6 TestFile.log 
the line number is 1
the line number is 2
the line number is 3
the line number is 4
the line number is 5
the line number is 6
the line number is 7
the line number is 8
the line number is 9
the line number is 10
the line number is 11
the line number is 12
the line number is 13
the line number is 14

#截去后6行,顯示剩余內(nèi)容


[root@Durian scripts]# head TestFile.log 
the line number is 1
the line number is 2
the line number is 3
the line number is 4
the line number is 5
the line number is 6
the line number is 7
the line number is 8
the line number is 9
the line number is 10

#當(dāng)沒有選項參數(shù)時,默認(rèn)顯示前10行

-v ,在首行打印文件名稱
[root@Durian scripts]# head -n 5 -v TestFile.log 
==> TestFile.log <==
the line number is 1
the line number is 2
the line number is 3
the line number is 4
the line number is 5

#在首行打印文件名稱

tail-n,顯示行數(shù)
[root@Durian scripts]# tail -n 4 TestFile.log 
the line number is 17
the line number is 18
the line number is 19
the line number is 20

#查看messages文件的最后4行內(nèi)容


[root@Durian scripts]# tail -n +5 TestFile.log 
the line number is 5
the line number is 6
the line number is 7
the line number is 8
the line number is 9
the line number is 10
the line number is 11
the line number is 12
the line number is 13
the line number is 14
the line number is 15
the line number is 16
the line number is 17
the line number is 18
the line number is 19
the line number is 20

#截去前4行,顯示剩余內(nèi)容


[root@Durian scripts]# tail TestFile.log 
the line number is 11
the line number is 12
the line number is 13
the line number is 14
the line number is 15
the line number is 16
the line number is 17
the line number is 18
the line number is 19
the line number is 20

#當(dāng)沒有選項參數(shù)時,默認(rèn)顯示最后10行

-f

tail -f /var/log/messages

#當(dāng)文件內(nèi)容有更新時,動態(tài)的顯示最新的內(nèi)容

-v ,在首行打印文件名稱
[root@Durian scripts]# tail -v -n 3 TestFile.log 
==> TestFile.log <==
the line number is 18
the line number is 19
the line number is 20

#在首行打印文件名稱


[root@Durian scripts]# head -n 15 TestFile.log |tail -n 5
the line number is 11
the line number is 12
the line number is 13
the line number is 14
the line number is 15

#查看中間行第11~15行的內(nèi)容

sed-n,與p一起使用
[root@Durian scripts]# sed -n '10p' TestFile.log 
the line number is 10
#查看第10行的內(nèi)容

[root@Durian scripts]# sed -n '11,14p' TestFile.log 
the line number is 11
the line number is 12
the line number is 13
the line number is 14

#查看第11~14行的內(nèi)容

vi
[root@Durian scripts]# vi +12 TestFile.log

#文件打開后,光標(biāo)直接定位在第12行

more
[root@Durian scripts]# more -4 TestFile.log 
the line number is 1
the line number is 2
the line number is 3
the line number is 4
--More--(19%)

#顯示前4行


[root@Durian scripts]# more +4 TestFile.log 
the line number is 4
the line number is 5
the line number is 6
the line number is 7
the line number is 8
the line number is 9
the line number is 10
the line number is 11
the line number is 12
the line number is 13
the line number is 14
the line number is 15
the line number is 16
the line number is 17
the line number is 18
the line number is 19
the line number is 20

#截去前3行,顯示剩余內(nèi)容

less
[root@Durian scripts]# less +4 TestFile.lo

#截去前3行,顯示剩余內(nèi)容



向AI問一下細節(jié)

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

AI