Unix提供了兩個工具查看不止文件的頭部和尾部。這個功能程序叫作more
,但有種更強大的變異體叫作less
(起初我認爲這是玩笑)。less
這個程序是交互性地,因此很難在輸出時捕獲,可是仍然爲你們提供了粗略地樣本以下:html
$ less sonnets.txt Shake-speare's Sonnets I From fairest creatures we desire increase, That thereby beauty's Rose might never die, But as the riper should by time decease, His tender heir might bear his memory: But thou contracted to thine own bright eyes, Feed'st thy light's flame with self-substantial fuel, Making a famine where abundance lies, Thy self thy foe, to thy sweet self too cruel: Thou that art now the world's fresh ornament, And only herald to the gaudy spring, Within thine own bud buriest thy content, And tender churl mak'st waste in niggarding: Pity the world, or else this glutton be, To eat the world's due, by the grave and thee. II When forty winters shall besiege thy brow, And dig deep trenches in thy beauty's field, sonnets.txt
less
程序地關鍵點在於它提供了幾種有用的方法讓你到文件的指定位置,例如使用箭頭向上或向下移動整行內容,按空格鍵移到頁面底部,按^F
向前移一頁(例如,)或者 ^B
向後移一頁。退出less
, 輸入q
(‘quit’簡寫)。spring
或許,less
最強大的能力是斜槓鍵/
了,它能讓你從文件開始一直搜索到結尾。例如,假設咱們要在sonnets.txt
文件中搜索'玫瑰'(插圖18),十四行詩裏最經常使用的意象之一。用less
來作就是輸入 /rose
(讀做「斜槓玫瑰」), 如Listing 15:less
Listing 15:使用
less
搜索字符串'rose'Shake-speare's Sonnets工具
I學習
From fairest creatures we desire increase,
That thereby beauty's Rose might never die,
But as the riper should by time decease,
His tender heir might bear his memory:
But thou contracted to thine own bright eyes,
Feed'st thy light's flame with self-substantial fuel,
Making a famine where abundance lies,
Thy self thy foe, to thy sweet self too cruel:
Thou that art now the world's fresh ornament,
And only herald to the gaudy spring,
Within thine own bud buriest thy content,
And tender churl mak'st waste in niggarding:
Pity the world, or else this glutton be,
To eat the world's due, by the grave and thee.uiIIthis
When forty winters shall besiege thy brow,
And dig deep trenches in thy beauty's field,
/rosegoogle
Listing 15中輸入/rose
後結果就是在該文件中高亮顯示第一次出現'rose'。你能夠接着輸入n
指引程序搜索下一個匹配,或者N
搜索上一個匹配。url
最後兩個必會的less
命令是G
,和1G
(1
後面接G
),分別地做用是移到文件末尾,和返回到開始。Table 4總結了我認爲最重要的鍵組合(即:我認爲你須要它才能變得厲害),可是若是你對其餘命令也很感興趣,請查看wiki對less的介紹rest
我很鼓勵你習慣使用less
工具查看文件。解鎖的新技能一樣適用於其餘應用;例如:手冊頁(1.3章)用的less
來作操做交互界面,因此經過學習less
你也能夠更好的在手冊頁中自由移動。
命令 | 描述 | 示例 |
---|---|---|
up & down arrow keys |
向上或向下移動一行 | |
空格鍵 |
移至上一頁 | |
⌃F |
移至上一頁 | |
⌃B |
移至下一頁 | |
G |
移至文件末尾 | |
1G |
回到文件開始 | |
/<string> |
在文件中搜索字符串 | /rose |
n |
移到下個匹配的搜素結果 | |
N |
移到上個匹配的搜索結果 | |
q |
退出less |
1.對sonnets.txt
文件運行less
。向後移動3頁再向前移動3頁。移到文件末尾,在回到開始,最後退出。
2.搜索字符串All
(區分大小寫)。查找前面幾個匹配項,再查找後面幾個匹配項。接着移至文件的開始,數匹配項,一直數到結尾。最後比較你數的結果和運行grep All sonnets.txt | wc
的結果是否相同。(咱們將在3.4章節中學習grep
)
3.使用less
和/
(斜槓),找到詩中'Let me not'開始的行。這首詩中有匹配的嗎?備註:按n
尋找下個匹配項(若是有的話)。附加題:聽聽這首詩的現代版和原始版,哪一個版本的韻律更好?
man
使用了less
,咱們如今能夠搜索手冊頁了。在ls
的手冊頁中搜索'sort'字符串,發掘經過文件大小排序的選項操做。什麼命令可讓文件以長表的形式列出,而且越大的文件顯示在底部?備註: ls -rtl
做爲模型。