Skip to content

Instantly share code, notes, and snippets.

@ibreathebsb
Created September 6, 2018 12:48
Show Gist options
  • Save ibreathebsb/5e5d74d075b51d9fbafd748ee999ead8 to your computer and use it in GitHub Desktop.
Save ibreathebsb/5e5d74d075b51d9fbafd748ee999ead8 to your computer and use it in GitHub Desktop.

sed

stream editor

@ibreathebsb
Copy link
Author

ibreathebsb commented Sep 6, 2018

sed [option] command

# 原则匹配内容的方法
1. 行数为单位 start, end   或者只有一行start 或者start, +N表示start开始的N行
2. first~step : 例如打印奇数行 sed -n '1~2p'
3. /regex/ : cat /etc/passwd | sed -n '/root/p'
4. 正则后面加!表示反选

# 删除1到5行
cat -n /etc/passwd | sed '1,5d'

# 删除10到最后行
cat -n /etc/passwd | sed '10,$d'

# append插入

# 在2-5行每行后面都插入######
cat -n /etc/passws | sed '2,5a #######'

# insert插入

# 在2-5行每行前面都插入######
cat -n /etc/passws | sed '2,5i #######'

# 整行替换
# 把1-10行替换为 ABCD

# print 查看几行
# 查看前三行
cat -n /etc/passwd |  sed -n '1,3p'

# 行内文本的替换

cat -n /etc/passwd |  sed '1,10s/root/someone/g'  # 把1-10行的root换成someone

# 直接修改文件
sed -i 'd1' ./text  # 将text文件的第一行删除

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment