sed 's/pattern/replace_string/' file # 选项-i会使得sed用修改后的数据替换原始文件 sed -i 's/text/replace/' file #以上只替换了每行中模式首次匹配的内容。g标记可以使sed执行全局替换 sed -i 's/pattern/replace_string/g' file #sed命令会将s之后的字符视为命令分隔符。这允许我们更改默认的分隔符 sed 's:text:replace:g' sed 's|text|replace|g' # 删除行 sed -i '/server/d' /etc/nginx/nginx.conf sed -i '/^$/d' file # 移除空行, 空行可以用正则表达式 ^$ 进行匹配 # &可指代模式所匹配到的字符串,能够在替换字符串时使用已匹配的内容 echo this is an example | sed 's/\w\+/[&]/g'# 正则表达式\w\+匹配每一个单词 # [this] [is] [an] [example] # 子串匹配标记(\1) echo this is digit 7 in a number | sed 's/digit \([0-9]\)/\1/'# \(pattern\)用于匹配子串. 对于匹配到的第n个子串,其对应的标记是\n # this is 7 in a number echo seven EIGHT | sed 's/\([a-z]\+\) \([A-Z]\+\)/\2 \1/' # EIGHT seven #利用管道组合多表达式. 以下三句含义相同: sed 'expression' | sed 'expression' sed 'expression; expression' sed -e 'expression' -e 'expression' echo abc | sed 's/a/A/' | sed 's/c/C/'# AbC echo abc | sed 's/a/A/;s/c/C/'# AbC echo abc | sed -e 's/a/A/' -e 's/c/C/'# AbC
awk 'NR==M, NR==N' filename seq 100 | awk 'NR==4,NR==6' # 打印位于模式start_pattern与end_pattern之间的文本 awk '/start_pattern/, /end_pattern/' filename #cat section.txt #line with pattern1 #line with pattern2 #line with pattern3 #line end with pattern4 #line with pattern5 awk '/pa.*3/, /end/' section.txt #line with pattern3 #line end with pattern4