2014年12月22日 星期一

SED & AWK

Sed

刪除
sed -i '/DeleteKeyWord/d' file
sed -i 'nd' file
n 為刪除的行數
截取關鍵字
sed -n '/KeyWord/w new_file' file
插入第n 行
sed -i 'niInsertWord' file
n 為插入第n 行
i 為插入命令
置換
sed 's/Pattern/ReplacePattern/g' file
s: 置換指令
g: 全域

Awk

....




2014年12月21日 星期日

Bash - 算術運算

參考於  http://www.360doc.com/content/11/0921/17/834950_150153766.shtml

1. 使用 expr
m=`expr 41 + 5`
m=`expr 41 - 5`
m=`expr 41 \* 5`
m=`expr 41 / 5`
m=`expr 41 % 5`
2. 使用 $(())
m=$((41 + 5))
m=$((41 - 5))
m=$((41 * 5))
m=$((41 / 5))
m=$((41 % 5))
3. 使用 $[]
m=$[41 + 5]
m=$[41 - 5]
m=$[41 * 5]
m=$[41 / 5]
m=$[41 % 5]
4. 使用 let
let m=41+5
let m=41-5
let m=41*5
let m=41/5
let m=41%5

Bash - 判斷輸入的參數是否為浮點數或整數

FileName: check_num.sh
指令: sh check_num.sh -i Num   or sh check_num.sh -f Num
說明: 利用正規表示法判斷

#! /bin/bash
floatExp="^(-?\d+)(\.\d+)?$"
intExp="^-?\d+$"
exp=""
exp=${intExp}
if [ $# != 2 ] ; then
 echo wrong arg nums,please input one arg
 exit 1
fi
case $1 in
"-f")
  exp=${floatExp}
        ;;
"-i")
  exp=${intExp}
        ;;
*)
   echo option shuld be -i or -f
   exit 1
        ;;
esac
if [ `echo $2|grep -P "${exp}"|wc -l` == 1  ] ; then
   echo num $2
   exit 0;
else
   echo not num
   exit 1;
fi


Bash - Date 格式

常用格式
date +"日期格式"
日期格式
date +"%Y %m-%d, %H:%M"
"_"  "-"  "^" 的用法:
"_"  : 0用空白填塞
"-"  :0不填塞
"^" :將輸出的小寫改為大寫
轉換為秒數
date -d "時間格式" +%s

日期變更
$ date --date="now"   // 現在時間
$ date --date="yesterday"  // 昨天
$ date --date="1 days ago"  // 昨天
$ date --date="3 days ago"  // 三天前
$ date --date="tomorrow" // 明天
$ date --date="1 days" // 明天
$ date --date="3 days" // 三天後

組合處理
date --date="3 days"  +"%Y %_m-%d, %a ,%H:%-M"
-d<字串>  顯示字串所指的日期與時間。字串前後必須加上雙引號。 
-s<字串>  根據字串來設置日期與時間。字串前後必須加上雙引號。 
SYNOPSIS
       FORMAT controls the output.  The only valid option for the second  form  specifies  Coordinated  Universal        Time.  Interpreted sequences are:        %%     a literal %        %a     locale's abbreviated weekday name (e.g., Sun)        %A     locale's full weekday name (e.g., Sunday)        %b     locale's abbreviated month name (e.g., Jan)        %B     locale's full month name (e.g., January)        %c     locale's date and time (e.g., Thu Mar  3 23:05:25 2005)        %C     century; like %Y, except omit last two digits (e.g., 21)        %d     day of month (e.g, 01)        %D     date; same as %m/%d/%y        %e     day of month, space padded; same as %_d        %F     full date; same as %Y-%m-%d        %g     last two digits of year of ISO week number (see %G)        %G     year of ISO week number (see %V); normally useful only with %V        %h     same as %b        %H     hour (00..23)        %I     hour (01..12)        %j     day of year (001..366)        %k     hour ( 0..23)        %l     hour ( 1..12)        %m     month (01..12)        %M     minute (00..59)        %n     a newline        %N     nanoseconds (000000000..999999999)        %p     locale's equivalent of either AM or PM; blank if not known        %P     like %p, but lower case        %r     locale's 12-hour clock time (e.g., 11:11:04 PM)        %R     24-hour hour and minute; same as %H:%M        %s     seconds since 1970-01-01 00:00:00 UTC        %S     second (00..60)        %t     a tab        %T     time; same as %H:%M:%S        %u     day of week (1..7); 1 is Monday        %U     week number of year, with Sunday as first day of week (00..53)        %V     ISO week number, with Monday as first day of week (01..53)        %w     day of week (0..6); 0 is Sunday        %W     week number of year, with Monday as first day of week (00..53)        %x     locale's date representation (e.g., 12/31/99)        %X     locale's time representation (e.g., 23:13:48)        %y     last two digits of year (00..99)        %Y     year        %z     +hhmm numeric timezone (e.g., -0400)        %:z    +hh:mm numeric timezone (e.g., -04:00)        %::z   +hh:mm:ss numeric time zone (e.g., -04:00:00)        %:::z  numeric time zone with : to necessary precision (e.g., -04, +05:30)

Bash - 檔案流水號製作

#!/bin/bash

GenerateFile=""
GetNum=1
BreakFlag=1
Today=`date +%Y%m%d`
while [ $BreakFlag -eq 1 ];
do
        tmpFileName="File_"$Today"_"$GetNum".log"

        echo "GetNum $GetNum"
        if [ -e $tmpFileName ]; then
                let GetNum+=1
                continue
        fi

        echo "filename $tmpFileName"
        GenerateFile=$tmpFileName
        break
done

echo "" > $GenerateFile

Bash - 迴圈的應用 - while & loop

while - 一般 條件判斷處理
while [ condition ]
do
   command1
   command2
   command3
done
while - 讀檔

while read LINE
do
用來做條件塞選或過濾用
done <  指定的檔案名稱
For Loop
for OUTPUT in $(Linux-Or-Unix-Command-Here)
do
 command1 on $OUTPUT
 command2 on $OUTPUT
 commandN
done
Array Loop
for idx in ${!Array[*]}
do
      echo "${Array[$idx]}"
done

for VARIABLE in 1 2 3 4 5 .. N
do
 command1
 command2
 commandN
done
for (( EXP1; EXP2; EXP3 ))
do
 command1
 command2
 command3
done
example:
#!/bin/bash
for i in {1..5}
do
   echo "Welcome $i times"
done
#!/bin/bash
for (( c=1; c<=5; c++ ))
do
   echo "Welcome $c times"
done
#!/bin/bash
for filename in $(ls *.log)
do
   echo "File Name $filename"
done

無窮迴圈
#!/bin/bash
for (( ; ; ))
do
   echo "infinite loops [ hit CTRL+C to stop]"
done


Bash - Array 的應用

Array  賦值
Array=( A B C D E F )
Array+=( G )
Array+=( H )
Array 複製
Array=${CopyArray[@]}
Array 給值
Array[0]=Value
Array[1]=Value
Array[2]=Value

N維 Array 複製
Array[0]=${CopyArray1[@]}

Array[1]=${CopyArray2[@]}
取得 ArraySize
${#Array[@]}
${#Array[*]}
Print Array 內容
${Array[@]}
${Array[*]}           <- 將值合併為一字串
Array 迴圈應用
for idx in ${!Array[*]}
do
      echo "${Array[$idx]}"
done