2017年10月20日 星期五

用 sed 在 xml 裡新增收件者

find * -name 'resource_*.xml' -exec grep -H -i "w33" {} \;|awk -F":" '{print "sed -i.bak -e \x27\\#w33#a\\\\x9\\x9<receiver user=\x22三五\x22 >w35@mail.XXX.XXX.tw</receiver>\x27 "$1 " "}'

* 印不出來的時候用 \x 代替

"  =  \x22
'   =  \x27
\t  =  \x9


* 在 sed 要換  delimiters 的時候前面要加一個 \

# do this (ugly)...
sed '/\/a\/b\/c\//{do something;}'
# ...or these (better)
sed '\#/a/b/c/#{do something;}'
sed '\_/a/b/c/_{do something;}'
sed '\%/a/b/c/%{do something;}'
# etc.

http://backreference.org/2010/02/20/using-different-delimiters-in-sed/

* grep  可以用  -H  印出檔名

* sed 要印 兩個 tab 的時候用  \\x9\x9 ,丟到 awk 裡面的話 前面各再加一個 \

* sed 真的要跑的時候加上 -i.bak -e 'SCRIPT' 就會真的改

* sed '/PATT/aNEW' 代表在 MATCH 到 PATT 這個 pattern 的後面一行加上 NEW



*sed 把換行符號拿掉

sed ':a;N;$!ba;s/\n/ /g' file
sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/ /g' file

https://stackoverflow.com/questions/1251999/how-can-i-replace-a-newline-n-using-sed


  1. Create a label via :a.
  2. Append the current and next line to the pattern space via N.
  3. If we are before the last line, branch to the created label $!ba ($! means not to do it on the last line as there should be one final newline).
  4. Finally the substitution replaces every newline with a space on the pattern space (which is the whole file).