Home | Mirror | Search

4. sed

http://sed.sourceforge.net/

sed -i -e 's/aaa/bbb/g' *
perl -p -i -e 's/aaa/bbb/g' *
	

4.1. find and replace

		
ls -1 *.html| awk '{printf "sed \047s/ADDRESS/address/g\047 %s >%s.sed;mv %s.sed %s\n", $1, $1, $1, $1;}'|bash

for f in `ls -1 *.html`; do [ -f $f ] && sed 's/<\/BODY>/<script src="http:\/\/www.google-analytics.com\/urchin.js" type="text\/javascript"><\/script>\n<script type="text\/javascript">\n_uacct = "UA-2033740-1";\nurchinTracker();\n<\/script>\n<\/BODY>/g' $f >$f.sed;mv $f.sed $f ; done;
		
		

		
my=/root/dir
str="/root/dir/file1 /root/dir/file2 /root/dir/file3 /root/dir/file/file1"
echo $str | sed "s:$my::g"
		
		
4.1.1. 正則
sed s/[[:space:]]//g  filename          刪除空格
			
4.1.2. delete

刪除空行

sed /^$/d         filename
sed '/./!d' filename
			
4.1.3. aaa="bbb" 提取bbb
			

$ echo "aaa=\"bbb\"" | sed 's/.*=\"\(.*\)\"/\1/g'
$ curl -s http://www.example.com | egrep -o '<a href="(.*)">.*</a>' | sed -e 's/.*href="\([^"]*\)".*/\1/'

			
			

Mac 地址轉換

echo 192.168.2.1-a1f4.40c1.5756 | sed -r 's|(.*-)(..)(..).(..)(..).(..)(..)|\1\2:\3:\4:\5:\6:\7|g'
			
comments powered by Disqus