sed center line

来源:互联网 发布:godaddy专享域名转入 编辑:程序博客网 时间:2024/06/01 10:04

#!/usr/bin/sed -f
    
     # Put 80 spaces in the buffer
     1 {
       x
       s/^$/          /
       s/^.*$/&&&&&&&&/
       x
     }
     1为地址,如果是第一行,则在hold space中保存80个空格
     # del leading and trailing spaces
     y/\t/ /
     将TAB换成空格
     s/^ *//
     s/ *$//
     去掉前后的空格
     # add a newline and 80 spaces to end of line
     G
     添加一个\n和80个空格
     # keep first 81 chars (80 + a newline)
     s/^\(.\{81\}\).*$/\1/
 假设文本长度为15
     15\n65    

     # \2 matches half of the spaces, which are moved to the beginning
     s/^\(.*\)\n\(.*\)\2/\2\1/
     此时15文本\n32空格32空格1空格
     替换后为:
     32空格15文本1空格

原创粉丝点击