机器人 救救瓜
Toggle navigation
Home
SCM-tool
Linux
Jenkins
SVN
other
About Me
Archives
Tags
shell 里 sed的用法
? shell ?
2019-01-29 15:36:08
284
1
0
gua_l
? shell ?
``` sed -i "/key/c\modify key line " file #把file 中带有 key 的所有行替换为modify key line ``` eg. ``` change_list=`cat temp3.txt ` for i in ${change_list} do if [[ $exp ]]; then sed -i "/${i}/c\\${i} <-- change" temp2.txt fi done ``` 在exit 0 前面插入一行:i ``` sed -i '/^exit 0/i \/sbin\/start ttyS0' /etc/rc.local ``` 在exit 0 后面插入一行:a ``` sed -i '/^exit 0/a \/sbin\/start ttyS0' /etc/rc.local ``` 处理同一个打印结果 中,取多行标题及关键字匹配行 ``` accurev show wspaces | head -n 5 ; accurev show wspaces | grep EPDG_R16 ``` 以上accurev show wspaces 执行了两次,在两次结果里各取结果 再拼接,效率较低 ``` sed -n '1,5p' sed -n '/xxxxx/p' sed -n '1,5p;/^EPDG_R16_xxxx/p' ``` 使用sed 将两个条件用分号串成多个条件,对结果进行“或关系”匹配 ``` accurev show wspaces | sed -n '1,5p;/^EPDG_R16_xxxx/p' Stream # | Target X-Action | | X-Action | | | WS Type Workspace Storage Host | | | | EOL EPDG_R16_xxxx /vob1/lcf1/EPDG_R16 nut.at.casachina 26628 2039262 2039262 1 0 ``` 匹配中的 \s \b 和 '//d' \s 匹配任意空格,包括空格 制表符 ^\s* 表示行开头的零个或个空字符 \b 表示单词到此结束。abc\b 只会匹配abc 不会匹配到acbde '//d' 删除匹配项所在和行 以下例子为删除开头是%admin 和%sudo 的行 ``` sed -i '/^\s*%admin\b/d' $SUDOERS sed -i '/^\s*%sudo\b/d' $SUDOERS ``` 持续更新......
Pre:
Linux grep 常用用法
Next:
Shell if 用法
1
likes
284
Weibo
Wechat
Tencent Weibo
QQ Zone
RenRen
Submit
Sign in
to leave a comment.
No Leanote account?
Sign up now.
0
comments
More...
Table of content
No Leanote account? Sign up now.