惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
Y
Y Combinator Blog
博客园 - 【当耐特】
V
Visual Studio Blog
GbyAI
GbyAI
V
V2EX
P
Proofpoint News Feed
Microsoft Azure Blog
Microsoft Azure Blog
Microsoft Security Blog
Microsoft Security Blog
D
DataBreaches.Net
Hugging Face - Blog
Hugging Face - Blog
A
About on SuperTechFans
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
N
Netflix TechBlog - Medium
aimingoo的专栏
aimingoo的专栏
B
Blog RSS Feed
量子位
MongoDB | Blog
MongoDB | Blog
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
Stack Overflow Blog
Stack Overflow Blog
小众软件
小众软件

方永、南天紫雲

linux透明代理 技术的边界 停机问题通俗说明 Rust与oracle、redis集群的纠结 学习能力的增长 微信调试的原语 mac外接移动硬盘安装ArchLinux linux的死机问题 linux下全键盘操作 漂亮又好用的bspwm linux本机透明代理 ssh、mosh、autossh linux不能待机又一例 从WordPress切换到Hugo 折腾电脑开机 linux系统QQ新思路 Arch Linux的字体渲染 svn管理之submin OpenWrt自动fucking墙記錄 二维码(qrcode)名片的一些事儿 用lua nginx module搭建一个二维码(qr code)生成器 CentOS 7 安裝註記 php程序連接MySQL时只能127.0.0.1而localhost無法連接的問題 ThinkPad之FAN_ERROR、hi fi聲卡 jetty多端口部署 OpenWrt簡單暴力限網 linux中cron之PATH变量 OpenWrt的WDS無線橋接模式 再敘OpenWrt下的rtl8187無線中繼 終端進化之二零一三
sed之模擬tail
zola · 2013-11-11 · via 方永、南天紫雲

sed以簡潔的語法,與vi一貫的風格,受許多人的衷愛。

sed有許多現成的語句組合,可以提供各種實用的功能,sed模擬tail,將一文件的所有行倒序打印,就是一例。

語句如下:

sed -n 'G;h;$p' this_file

this_file 中第1行是 1 ,第2行是 2 , … ,第10行是 10 ,那麼這條語句將會打印出:

10
9
8
7
6
5
4
3
2
1

看起來很神奇,理解起來其實也很簡單。

首先要知道,sed有兩個緩存區,一個存放讀入的文件當前行的內容,稱爲 pattern buffer,一個稱作hold space,可存放一些自定義的內容。

如上面 this_file ,sed讀入第1行的內容 1 ,那麼 pattern buffer的內容就是 1G 命令後,pattern buffer的內容變爲 1\n\n 爲換行符, G 命令的作用是把hold space的內容追加到pattern buffer後面,然後是命令 ;; 的作用類似於其它語言,不過更爲原始,意爲“繼續將 ;後面的命令作用於當前的pattern bufferhold spaceh 命令將複製pattern buffer中的內容到hold space,替換了原來hold space中的內容,這樣, h 命令之後hold space的內容就是 1\n$p 的作用是在最後一行時打印pattern buffer中的內容, $ 代表最後一行的行號,爲 p 命令指定作用範圍, p 的作用是打印pattern buffer中的內容。這樣,在第1行,整個命令組合執行完,sed不會有任何輸出,pattern buffer中的內容是 1\nhold space中的內容也是 1\n

然後到第2行,pattern buffer中的內容是 2G 命令把hold space的內容追加到pattern buffer後面,那麼 G 命令後pattern buffer中的內容爲 2\n1\nh 命令後hold space的內容爲 2\n1\n 。如此反覆,到最後一行,pattern buffer的內容爲 10\n9\n8\n7\n6\n5\n4\n3\n2\n1\n ,這時 h 仍會複製pattern buffer中的內容到hold space,不過已沒什麼作用,而 $p 將會執行,打印出pattern buffer的內容,作爲sed唯一的輸出。

sed的 -n 選項說明不去打印 pattern buffer的內容。