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
原文地址:https://www.cnblogs.com/xsj24/p/4369891.html