2013年1月3日 星期四

sed -- 取出 pattern 中的某個部份

例如:從 XML 檔中取出某 tag 的值。
<name>Charles</name> <id>123</id> <color>red</color> ...

用 sed 來操作,就是...
  • 找到 符合 pattern 的地方
  • 印出 pattern 的某個部份

以上面的例子,xml tag 的 pattern 和 value就是: <.*> \(.*\) <\/.*>
前後被 tag 包起來,中間的部份要留下來 \(.*\)

把這個放到 sed 控制字串中: sed 's/ <.*>\(.*\)<\/.*> / \1 /' file 一樣用 s 指令。
最後用 \1 代表 pattern 中,第一個 \(.*\) 的內容。

這樣所有的 tag 都會被取出。
如果只是要某個 tag,例如 : id。
就可以把 id 寫入 pattern 中: sed 's/ <id>\(.*\)<\/id> / \1 /' file





2012年10月2日 星期二

string array

類似 array 的作法,在 shell script 中是用 space 作分別。
像: STRINGLIST="a b c_d e,f" for ele in $STRINGLIST do echo $ele done
結果output 就是: a b c_d e,f

所以要一個一個加string array 內容是.. SARY="a" SARY=$SARY" b" SARY=$SARY" c" echo $SARY
結果會是: a b c

2012年8月20日 星期一

create large file with dd command

要一個大的 file 作 loop back。
要先 create。

以往都是用: dd if=/dev/zero of=10G bs=1G count=10 這樣會花一些時間,硬碟把 00 寫入 10G 的 file。


要是不管file 的內容,只要10G 大小,可以用: dd if=/dev/zero of=10G bs=1 count=0 seek=10G 這樣馬上就可以create 出來。


  • http://www.skorks.com/2010/03/how-to-quickly-generate-a-large-file-on-the-command-line-with-linux/

2012年7月23日 星期一

copy files according the list file

其實這是 moore 的 script。
他把各系統要 copy 的 filename 寫在同一個 list file 中,每個 filename 用 ≶ > 刮起來。
仿造 xml 格式,裡面是 系統名稱。
<N_Series>MyBrowser.apk</N_Series>

所以要寫一個 script,依照這個 list 內容來 copy files..




要注意那個從 filelist xml 中剔除 tag 的 sed,要先把 </...> 剔除,再剔除開頭的。
否則因為整個 entry 剛好被包圍,所以會被整個剔除。

2012年7月22日 星期日

mount , 並指定 uid, gid 和 charset

mount 並且設定 mount folder 的 uid, gid, (or iochartset).

都是用 -o

uid, gid:
# mount -o uid=charles-chang,gid=charles-chang /dev/sdd1 ~/sd

如果是要 mount windows 的 share (samba, cifs),中文的問題可以用: iocharset:
mount -t cifs -o username=softwaretest,password=softwaretest,iocharset=utf8,uid=charles-chang,gid=charles-chang //192.168.147.225/ODM3 /home/charles-chang/buildingmachine

兩個 -o 可以何在一起,中間用 ',' 隔開

2012年5月17日 星期四

find .. then cd to it(them)

找到含 .git 的目錄,cd 過去,run git status..

ref: http://www.linuxquestions.org/questions/programming-9/shell-script-to-cd-into-the-result-of-find-592503/

#/bin/sh find . -type d -name '.git' | while read F; do D=$F/../ cd "$D" echo $PWD git status cd - > /dev/null echo --------------- done 找到的 path 含 .git,所以要cd 到 .git 上一層。
做完後,回到剛剛的目錄: cd - 不要把 cd - 的 command 印(echo) 出來,所以加上 > /dev/null

2012年4月24日 星期二

string operation -- replace file extension name

如果只是簡單的filename 操作,可以用 "${filename...}"
例如:把所有 mp4 檔 rename 成 avi: for file in *.mp4;do mv "$file" "${file/.mp4/.avi}";done
像這個 ${file/.mp4/.avi} 就是把 $file 中 .mp4 換成 .avi,其中的兩個 "/" 符號就是 replace。


ref:
  1. http://www.thegeekstuff.com/2010/07/bash-string-manipulation/
  2. http://www.faqs.org/docs/abs/HTML/string-manipulation.html