wiki:waue/2009/0521

Version 2 (modified by waue, 16 years ago) (diff)

--

  • 用shell 批次改檔名
    • 此用法應用到 for in , 正規表示法, awk , sed 因此還學滿多的
      #!/bin/sh
      cd ~/test2
      echo `pwd`
      for i in *.[tT][iI][fF]; do
              #若目錄內無 .tif 檔案 $i 會傳回 "*.[tT][iI][fF]"
              if [ "$i" != "*.[tT][iI][fF]" ]; then
                      #取得副檔名
                      subname="`echo $i | awk -F '.' '{print $NF}'`"
                      #取得主檔名
                      filename="`echo $i | sed -e s/\.${subname}//`"
                      #將 .tif 轉成 .pdf
                      #tiff2pdf -o "${filename}.pdf" "$i"
                      echo "tif -> pdf"
                      mv ${filename}.${subname} ${filename}.pdf
                      #若執行成功則刪除 .tif 檔案
                      if [ $? -eq 0 ]; then
                              rm -rf $i
                      fi
              fi
      done
      
  • awk
    Examples:
    	gawk '{ sum += $1 }; END { print sum }' file
    	gawk -F: '{ print $1 }' /etc/passwd
    
  • sed
    Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
      -n, --quiet, --silent
                    suppress automatic printing of pattern space
      -e script, --expression=script
                     add the script to the commands to be executed