close

 

 

test input: TEST

aa bb cc dd = 20

aa bb cc dd = 25

aa bb cc ee = 5

aa bb cc ee + 10

aa bb cc dd = 30

 

1. 列出所有包涵 "dd" 的資料行:

=> grep "dd" TEST

延伸: grep -irn "dd" ./xx/xxx

 

2. 取出所有 "=" 之後的數字:

grep "=" TEST | awk -F"= " '{print $2}'

or 

grep -v "+" TEST | awk -F"= " '{print $2}'

 

3. 取出所有 "=" 之後, 且大於20的數字:

grep "=" TEST | awk -F"= " '$2 > 20 {print $2}'

 

4. "="之後的數字相加:

grep "=" TEST | awk -F"= " '{sum+=$2} END {print sum} '

 

5. 取出所有 "=" 之後, 且介於20到30之間的數字:

grep "=" TEST | awk -F"= " '$2 >= 20 && $2 <=30 {print $2}' 

 

6. "="之後的數字平均: 

grep "=" TEST | awk -F"= "  '{sum+=$2} END {print sum/NR} '

 

7. sample script:

7-1. 使用 script 去存取操作一個檔案, 若無input, 則用 default.log

INPUT=default.log

if [  $# != 0 ]

then

        INPUT=$1

fi

 

7-2. 每30秒觀察某個 process 的 CPU loading, memory usage 和 fd usage

ps 得到該 process 的 PID -> 123

7-2-1. CPU loading:

TTY=$(tty)

while true

do

top -n 1 | grep -v grep | grep XXX | awk '{print $1}' > $TTY

sleep 30

done

 

7-2-2. memory usage:

TTY=$(tty)

while true

do

pmap -x 123 | grep XXX | awk '{sum+=$2} END {print sum}' > $TTY

sleep 30

done

 

7-2-3. fd usage:

TTY=$(tty)

while true

do

ls -al /proc/123/fd | grep socket | wc -l > $TTY

sleep 30

done

 

7-3. 每30秒觀察某個 process 的 CPU loading, memory usage 和 fd usage,

再把這些資訊寫在 result.txt 的末端

CpuFile=cpu.txt

MemoryFile=memory.txt

FdFile=fd.txt

ResultFile=result.txt

 

while true

do

 

touch $CpuFile

touch $MemoryFile

touch $FdFile

 

top -n 1 | grep -v grep | grep XXX | awk '{print $1}' > $CpuFile

pmap -x 123 | grep XXX | awk '{sum+=$2} END {print sum}' > $MemoryFile

ls -al /proc/123/fd | grep socket | wc -l > $FdFile

 

cat $CpuFile >> $ResultFile

cat $MemoryFile >> $ResultFile

cat $FdFile >> $ResultFile

 

rm $CpuFile

rm $MemoryFile

rm $FdFile

 

sleep 30

done

 

8. 在所有./以下的 .c檔 尋找 xxx 字串

find ./ -name \*.c -exec grep -irnH "xxx" {} \;

-i: 不分大小寫

-r: 遞迴

-n: 行數

-l: 只顯示符合規則的檔案名稱

-H: 檔案+路徑

 

9. 在  ./  之下尋找所有名為 "xxx"  的資料夾

find ./ -type d -name "xxx"

 

10. (8) 的延伸

若想找到符合某 pattern 的檔案並移到某路徑

find ./xxx -name "\*.x*" -exec cp "{}" ../../xxxxx  \;

https://stackoverflow.com/questions/5241625/find-and-copy-files

 

arrow
arrow
    全站熱搜

    kk 發表在 痞客邦 留言(0) 人氣()