Linux sed Tips
来源:互联网 发布:hd4800玩守望怎么优化 编辑:程序博客网 时间:2024/06/05 14:40
sed常见格式:
sed -option 'command/pattern1/pattern2/pattern flag' 1.txt
-option包括:
-n: noprint。常与pattern flag: p合用
-e: 执行多个sed命令
-f: 调用文件中的sed脚本
-i: 编辑文件。例子:
[oracle@odilab ~]$ cat 1.txtabcdef[oracle@odilab ~]$ echo "," >> 1.txt[oracle@odilab ~]$ sed -i 's/,//' 1.txt[oracle@odilab ~]$ cat 1.txtabcdef
command:
s: 替换
d: 删除
a: 追加 append
n: next
pattern flag:
g: global
p: print
i: ignore case
w file: write to file,将会覆盖原有内容。请看例子。
[oracle@odilab ~]$ sed -n '/a/w 2.txt' 1.txt[oracle@odilab ~]$ cat 2.txtaa将最后一行写入2.txt文件
[oracle@odilab ~]$ sed -n '$w 2.txt' 1.txt[oracle@odilab ~]$ cat 2.txta与-e搭配,将不同内容写入2.txt. 这个方法不是最优的,仅仅为了说明命令用法。
[oracle@odilab ~]$ sed -n -e '/a/w 2.txt' -e '/b/w 2.txt' 1.txt[oracle@odilab ~]$ cat 2.txtaba
--Removing the last comma from each line.
sed 's/,$//g' xxx.file
--执行文件中的sed命令
sed -f <file> < <input.txt>
--打印指定行 /p参数
sed默认情况下打印整个文件,-n将只打印匹配行。
[oracle@odilab ~]$ cat 1.txtabaa[oracle@odilab ~]$ sed '1p' 1.txtaabaa[oracle@odilab ~]$ sed -n '1p' 1.txta[oracle@odilab ~]$ sed -n '1,2p' 1.txt1318932 ./4.txt1270876 ./data/IA_Adjustments.dat.good
-n = noprint
-n , p 组合相当于grep命令这里'1,2p' 表示打印第1到第2行
--删除匹配的行
--打印行号 =参数
该功能用grep实现更简单:
--删除匹配的行
[oracle@odilab ~]$ sed '/a/d' 1.txtb注:文件并没有被删除,只是在屏幕上显示而已。
[oracle@odilab ~]$ sed '/a/d' 1.txt > 2.txt[oracle@odilab ~]$ cat 2.txtb这时,结果被写入另一个文件。
--打印行号 =参数
[oracle@odilab ~]$ cat 1.txtabcdefa[oracle@odilab ~]$ sed -n '/a/{=;p}' 1.txt |sed '{N;s/\n/ /}'1 a7 a= 打印的行号和文件内容不在同一行。需要N参数将下一行读入,然后将换行符替换为空格。
该功能用grep实现更简单:
[oracle@odilab ~]$ grep -n "a" 1.txt1:a7:a
0 0
- Linux sed Tips
- Sed Tips and Tricks
- sed tips学习
- linux sed
- linux sed
- linux sed
- linux sed
- linux sed
- Linux sed
- linux sed
- linux sed
- linux sed
- Linux -- sed
- linux sed
- linux sed
- linux sed
- linux sed
- linux sed
- Oracle Linux 6.4配置NFS
- Oracle Linux xargs Command
- Linux awk Tips
- 慎用USES_CONVERSION
- 慎用USES_CONVERSION
- Linux sed Tips
- Linux Ctrl Z
- SELinux tips
- Setting up Samba3.6.9 on Oracle Linux 6
- 关于STL/ATL/WTL之间的区别
- 设计模式之 动态代理 - ThreadLocal实现事务管理
- ubuntu无线网卡驱动安装
- extern "c" __declspec(dllexport) __declspec(dllimport)
- Getting Approximate Row Count of a Huge File