vim + sed 로 셸 스 크 립 트 파일 정리 하기

   
在工作中遇到的问题,此nginx.sh配置脚本是我在Googel下载下来的,实际复制贴�到服务器中运行时发现前面都有 001-100的行标识符,外带空格,影响运行和美观啊;本来想一行行删除,后来不禁自己也鄙视自己:这也太没技术含量了吧?具体解决方法如下:
①首先在 vim里删除所有行首数字,如下所示:
1    :%s/^[0-9][0-9]* //    
②然后再删除所有行首空字符,如下所示:
1    sed -i  's/^[[:space:]]*//' nginxd.sh    
整个nginxd.sh脚本如下,有兴趣的朋友也拿着练下手。
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100    001     #!/bin/sh002    003     # source function library004     . /etc/rc.d/init.d/functions005    006     # Source networking configuration.007     . /etc/sysconfig/network008    009     # Check that networking is up.010     [ ${NETWORKING} = "no" ] && exit 0011    012     RETVAL=0013     prog="nginx"014    015     nginxDir=/usr/local/nginx016     nginxd=$nginxDir/sbin/nginx017     nginxConf=$nginxDir/conf/nginx.conf018     nginxPid=$nginxDir/nginx.pid019    020     nginx_check()021     {022         if [[ -e $nginxPid ]]; then023             ps aux |grep -v grep |grep -q nginx024             if (( $? == 0 )); then025                 echo "$prog already running..."026                 exit 1027             else028                 rm -rf $nginxPid &> /dev/null029             fi030         fi031     }032    033     start()034     {035         nginx_check036         if (( $? != 0 )); then037             true038         else039             echo -n $"Starting $prog:"040             daemon $nginxd -c $nginxConf041             RETVAL=$?042             echo043             [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx044             return $RETVAL045         fi046     }047    048     stop()049     {050         echo -n $"Stopping $prog:"051         killproc $nginxd052         RETVAL=$?053         echo054         [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx $nginxPid055     }056    057     reload()058     {059         echo -n $"Reloading $prog:"060         killproc $nginxd -HUP061         RETVAL=$?062         echo063     }064    065     monitor()066     {067         status $prog &> /dev/null068         if (( $? == 0 )); then069             RETVAL=0070         else071             RETVAL=7072         fi073     }074    075     case "$1" in076             start)077                     start078                     ;;079             stop)080                     stop081                     ;;082             restart)083                     stop084                     start085                     ;;086             reload)087                     reload088                     ;;089             status)090                     status $prog091                     RETVAL=$?092                     ;;093             monitor)094                     monitor095                     ;;096             *)097                     echo $"Usage: $0 {start|stop|restart|reload|status|monitor}"098                     RETVAL=1099     esac100     exit $RETVAL    
另注:如果单纯只用sed来执行的话就比较简单了,如下所示的一句命令就可以搞定需求了:
1    sed -i -e 's@^[0-9][0-9]*@@ ' -e 's@^[[:space:]]* @@' nginx.sh    
本文出自 “抚琴煮酒” 博客,请务必保留此出处http://yuhongchun.blog.51cto.com/1604432/504993

좋은 웹페이지 즐겨찾기