sed 常见用法

sed

1. 移除空白行

sed '/^$/d' file

2. 直接在文本中进行替换

sed 's/pattern/replacement/g' -i file

-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)

3. 已匹配字符串标记 &

可以使用&标记匹配样式的字符串, 这样可以在替换字符串时使用已匹配的内容

echo 'this is an example' | sed 's/\w\+/[&]/g'
结果: [this] [is] [an] [example]

4. 子串匹配标记 \1

echo 'this is digit 7 in a number' | sed 's/digit \([0-9]\)/\1/'
结果: this is 7 in a number

5. 删除前五行数据

sed '1,5d' -i file

6. 删除最后一行数据

sed '$d' -i file
posted @ 2015-03-26 20:02  邓晓锋  阅读(193)  评论(0编辑  收藏  举报