<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Natural

     

    [demo]sed字符串替換

    找出Z:\A、Z:\B目錄下所有內容帶“APPLE“的文件,把"APPLE"替換成"ORANGE",并輸出到Z:\C相應的目錄下

      1 #!/bin/sh
      2 # Auto replace the files' content and copy them to the specified directory.
      3 
      4 SEARCH_WORD='APPLE'
      5 REPLACE_WORD='ORANGE'
      6 
      7 SRC_DIR_ARRAY=(
      8 'Z:/A'
      9 'Z:/B'
     10 )
     11 
     12 OUTPUT='Z:/C'
     13 
     14 global_lastDir_pre=''
     15 declare -i count=0
     16 
     17 function checkFileDir
     18 {
     19     local fileNameWithPath=$1
     20     local dirPre=${fileNameWithPath%/*}
     21     local tempPath=${OUTPUT}/${dirPre}
     22     global_lastOutput=$OUTPUT/$fileNameWithPath
     23     
     24     if [ ! -"$tempPath" ]
     25     then
     26         tar -cf - "$fileNameWithPath" | tar -"$OUTPUT" -xf -
     27         rm -"$global_lastOutput"
     28     fi
     29 
     30 }
     31 
     32 function replaceFile
     33 {
     34     local fileNameWithPath=${1#*/}
     35     local regix=$2
     36     local thisDir_pre=${fileNameWithPath%/*
     37     
     38     if [ "$thisDir_pre" = "$global_lastDir_pre" ]
     39     then
     40         sed "$regix" "$fileNameWithPath" > "$global_lastOutput"
     41         continue
     42     fi
     43 
     44     checkFileDir "$fileNameWithPath"
     45     global_lastDir_pre=$thisDir_pre
     46 
     47     sed "$regix" "$fileNameWithPath" > "$global_lastOutput"
     48 }
     49 
     50 function echoNote
     51 {
     52     echo "Wrong directory detected. Please check it !"
     53 }
     54 
     55 function init
     56 {
     57     for src_dir in ${SRC_DIR_ARRAY[*]}
     58     do
     59         if [ ! -"$src_dir" ]
     60         then
     61             echoNote
     62             echo "SRC_DIR_ARRAY: '$src_dir'"
     63             exit
     64         fi
     65     done
     66     
     67     if [ ! -"$OUTPUT" ]
     68     then
     69         echoNote
     70         echo "OUTPUT: '$OUTPUT'"
     71         exit
     72     fi
     73     
     74     rm -rf "$OUTPUT"
     75     mkdir "$OUTPUT"
     76 
     77 }
     78 
     79 
     80 echo -"----------------------------BEGIN--------------------------------\n"
     81 
     82 init
     83 for src_dir in ${SRC_DIR_ARRAY[*]}
     84 do
     85 
     86     cd "$src_dir"
     87     list=`find . -type f | xargs grep -"$SEARCH_WORD"`
     88     #list=`find . -type f | xargs grep -il "$SEARCH_WORD"`    # ignore case
     89     for line in $list
     90     do
     91         grep -Hn "$SEARCH_WORD" "$line" >> "$OUTPUT/log.txt"
     92         #grep -iHn "$SEARCH_WORD" "$line" >> "$OUTPUT/log.txt"    # ignore case
     93         count=count+1
     94         echo $line
     95         replaceFile "$line" "s/$SEARCH_WORD/$REPLACE_WORD/g"
     96         #replaceFile "$line" "s/$SEARCH_WORD/$REPLACE_WORD/Ig"    # ignore case
     97     done
     98 
     99 done
    100 
    101 echo -"\n----------summary----------"
    102 echo "total files size: $count"
    103 
    104 echo "----------------------------END--------------------------------"
    105 

    posted on 2012-02-21 15:51 此號已被刪 閱讀(348) 評論(0)  編輯  收藏 所屬分類: Linux

    導航

    統計

    常用鏈接

    留言簿(8)

    隨筆分類(83)

    隨筆檔案(78)

    文章檔案(2)

    相冊

    收藏夾(7)

    最新隨筆

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: rh男男车车的车车免费网站| 亚洲人成依人成综合网| 亚洲精品一卡2卡3卡四卡乱码| 2019中文字幕在线电影免费| 亚洲国产精品婷婷久久| 午夜精品一区二区三区免费视频| 亚洲AV无码专区国产乱码4SE| 热久久这里是精品6免费观看| 亚洲国产精品无码AAA片| 无码国产精品一区二区免费3p | 亚洲AV午夜成人片| 免费h视频在线观看| 久久亚洲国产成人精品性色| 国产精品1024永久免费视频| 亚洲国产精品成人精品小说| 成人免费视频观看无遮挡| 亚洲日韩一中文字暮| 又粗又大又长又爽免费视频| 特色特黄a毛片高清免费观看| 久久久久国产亚洲AV麻豆| 另类免费视频一区二区在线观看| 亚洲日韩区在线电影| 成人黄动漫画免费网站视频| 国产午夜亚洲精品不卡| 亚洲精品无码不卡在线播HE| 亚洲免费在线观看视频| 亚洲熟女综合色一区二区三区| 国产中文字幕免费观看| a成人毛片免费观看| 亚洲人成网站在线观看播放青青| 在线视频免费观看www动漫| caoporn国产精品免费| 亚洲人成亚洲精品| 国产成人一区二区三区免费视频| 国产免费区在线观看十分钟| 久久久久亚洲精品日久生情| 国产精品免费看久久久无码| 国产免费AV片在线观看| 亚洲精品精华液一区二区| 国产成人精品日本亚洲专区61| 国产精品久久永久免费|