sed学习笔记(六)--总结与一行脚本
时间:2007-12-21 来源:kinwin
首先还是sed的工作流程
sed默认是在所有的命令执行完后输出当前pattern space的内容。用n选项可以跳过这一步。
一些直接输出的命令:
a,i和c在处理两地址范围时的不同处理方式
小写和大写
pattern space每次循环都会刷新。hold space具有记忆功能。
一些一句话sed脚本
统计行数(wc -l)
反转行序(tac)
反转一行的字符顺序
输出奇数行
给每一行加上行号
给非空行加行号
显示一个文件最后5行
sed maintains two data buffers: the active pattern space, and the auxiliary hold space. Both are initially empty. sed operates by performing the following cycle on each lines of input: first, sed reads one line from the input stream, removes any trailing newline, and places it in the pattern space. Then commands are executed; each command can have an address associated to it: addresses are a kind of condition code, and a command is only executed if the condition is verified before the command is to be executed. When the end of the script is reached, unless the -n option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed.Then the next cycle starts for the next input line. Unless special commands (like `D') are used, the pattern space is deleted between two cycles. The hold space, on the other hand, keeps its data between cycles (see commands `h', `H', `x', `g', `G' to move data between both buffers). |
一些直接输出的命令:
p 输出当前pattern space中的内容 |
l 同p,但是也会显示一些控制字符 = 直接输出当前行号 q,n 默认输出当前pattern space的内容,用n选项可以跳过 w file 输出pattern space到file |
|
小写和大写
一般小写表示覆盖,大写表示追加 小写针对全体,大写针对第一行 |
一些一句话sed脚本
统计行数(wc -l)
sed -n '$=' |
sed -n '1!G;$p;h' |
sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//' |
sed 's/^.*$/\n&\n/;:x s/\(\n.\)\(.*\)\(.\n\)/\3\2\1/;tx;s/\n//g' 经实验,第二种方法效率更高。可以用time试一下 |
sed 'n;d' |
sed -n '1~2p' |
sed -n 'N;P' |
sed 'x;$!N;x' |
sed '=' filename | sed 'N;s/\n/\t/' |
sed '/./=' filename | sed '/./N;s/\n/\t/' |
sed -e :a -e '$q;N;6,$D;ba' |
相关阅读 更多 +
- 系统休眠文件删除后果 如何删除计算机的休眠文件 2025-04-22
- 站群服务器是什么意思 站群服务器的作用 站群服务器和普通服务器的区别 2025-04-22
- jQuery插件有何作用 jQuery插件的使用方法 2025-04-22
- jQuery插件有哪些种类 简单的jQuery插件实例 2025-04-22
-