-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sed #18
Comments
macmac中的所有 |
示例一:#!/bin/bash
# create temporary folder
mkdir -p temp/modules
# ng-annotate modules/*.js, the output is temp/modules/*.js
files=""
for file in $( find modules -type f -name "*.js" )
do
echo ng-annotate -a $file -o temp/"$file"
files=$files" temp/"$file
ng-annotate -a $file -o temp/"$file"
# Do something else.
done
# uglify temp/modules/*js, the output is app.js
echo uglifyjs -cmo app.js $files
uglifyjs -cmo app.js $files
# remove temporary folder
rm -rf temp/modules
start=$( grep -n 'MODULES_START' ./index.html|cut -d: -f1 )
end=$( grep -n 'MODULES_END' ./index.html|cut -d: -f1 )
diff=$[$end-$start]
echo $diff
if [ "$diff" -gt 1 ]; then
sed -i '' $[$start+1],$[$end-1]''d index.html
fi
sed -i '' '/MODULES_START/ a\
<script src="app.js"></script>\
' index.html
echo complete.
exit 0 示例二#!/bin/bash
files=""
for file in $( find modules -type f -name "*.js" )
do
files=$files'<script src="'$file'"></script>'
done
echo $files
start=$( grep -n 'MODULES_START' ./index.html|cut -d: -f1 )
end=$( grep -n 'MODULES_END' ./index.html|cut -d: -f1 )
diff=$[$end-$start]
echo $diff
if [ "$diff" -gt 1 ]; then
sed -i '' $[$start+1],$[$end-1]''d index.html
fi
targetstring="<!-- MODULES_END -->"
sed -i '' "s#${targetstring}#${files}#g" index.html
sed -i '' '/<\/body>/ i\
<!-- MODULES_END -->\
' index.html
echo complete.
exit 0 示例三:#!/bin/sh
#set web project path
XX_HOME=$HOME/ws/xx
#set firefox add-on sdk path
SDK_HOME=$HOME/DevTools/addon-sdk-1.17
#activate cfx command
cd $SDK_HOME
. ./bin/activate
#change to add-on directory
cd -
#define version number
CUR_VERSION=$(sed -n '/"version":/p' package.json|awk 'BEGIN{FS="\""}{print $4}')
echo "########Set release number#################"
echo -n "Please input new version(default: $CUR_VERSION):"
read NEW_VERSION
if [ -e $NEW_VERSION ]; then
NEW_VERSION=$CUR_VERSION
fi
echo "New version is: $NEW_VERSION"
#change package.json version
sed -i "s/$CUR_VERSION/$NEW_VERSION/" package.json
#change xx.properties version
sed -i '/XXAddOn\.requiredVersion/ cXXAddOn.requiredVersion='$NEW_VERSION $XX_HOME/data/xx.properties
#change monitorURL
sed -i '/var monitorURL/ cvar monitorURL = "/path/to/xx/";' lib/main.js
#build and release the plugin
cfx xpi && cp *.xpi $XX_HOME/WebContent/js/addon && echo "Build complete."
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
目录:
http://www.thegeekstuff.com/tag/sed-tips-and-tricks
打印。。
http://www.thegeekstuff.com/2009/09/unix-sed-tutorial-printing-file-lines-using-address-and-patterns/
删除
http://www.thegeekstuff.com/2009/09/unix-sed-tutorial-delete-file-lines-using-address-and-patterns/
查找替换
http://www.thegeekstuff.com/2009/09/unix-sed-tutorial-replace-text-inside-a-file-using-substitute-command/
学到了如何将windows下的换行批量转unix
sed 's/.$//' filename
这个.号也可以显示地写成\r,因为每行在处理之前会先自动去掉\n所以每行的最后一个字符是\r
写文件
http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-write-to-a-file-using-sed/
学到了同时执行多段脚本:
如何执行多个sed command
http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-execute-multiple-sed-commands/
#sed -e 'command' -e 'command' filename
如果只有一个command则-e可写可不写。(难怪)
删除第一行, 最后一行和空白行
高级替换
6-1如果REGEX或REPLACEMENT中有/,则需要用\转义,或者使用@ % | ; : 做为sed的分隔符
以上用@做为分隔符替换某个路径值
6-2 &用在REPLACEMENT指代REGEX实际匹配的那部分内容
这里的&指代/usr/bin, 又比如
把每行用<<<>>>括起来。。这个用法比较实用
6-3分组和back refercence
A group is opened with “(” and closed with “)”, 并且back reference即可用在REGEX部分也可以用在replacement部分
每行只保留第一个冒号前面的路径
==> /usr/kbos/bin:/usr/local/bin:/usr/jbin/ 变成 /usr/kbos/bin
又比如:
颠倒最后一行3个路径的出现顺序,
又比如
从/etc/passwd中取所有的用户名(第一列就是)
又比如
将每个单词首字母用括号括起来,我试了一下,这个例子中replacement部分括号是不需要转义的
这样也行
==> echo "Welcome To The Geek Stuff" | sed 's/\(\b[A-Z]\)/(\1)/g'
插入行,替换行,打印行号
http://www.thegeekstuff.com/2009/11/unix-sed-tutorial-append-insert-replace-and-count-file-lines/#count_lines
比如我有一个文件hello.sh内容如下:
echo "hello cyper"
使用后插a,前插i,和替换c之后的效果如下:
当然我的本意是前插, 所以在这种情况下选择i
同时操作多行 (待读)
http://www.thegeekstuff.com/2009/11/unix-sed-tutorial-multi-line-file-operation-with-6-practical-examples/
1.
http://www.thegeekstuff.com/2009/12/unix-sed-tutorial-7-examples-for-sed-hold-and-pattern-buffer-operations/
http://www.thegeekstuff.com/2009/12/unix-sed-tutorial-6-examples-for-sed-branching-operation/
The text was updated successfully, but these errors were encountered: