Skip to content
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

Open
uniquejava opened this issue Jan 6, 2016 · 2 comments
Open

sed #18

uniquejava opened this issue Jan 6, 2016 · 2 comments

Comments

@uniquejava
Copy link
Owner

目录:

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/

# sed -n 'ADDRESS'p filename
# sed -n '/PATTERN/p' filename

删除

http://www.thegeekstuff.com/2009/09/unix-sed-tutorial-delete-file-lines-using-address-and-patterns/

# sed 'ADDRESS'd filename
# sed /PATTERN/d filename

查找替换

http://www.thegeekstuff.com/2009/09/unix-sed-tutorial-replace-text-inside-a-file-using-substitute-command/

#sed 'ADDRESSs/REGEXP/REPLACEMENT/FLAGS' filename
#sed 'PATTERNs/REGEXP/REPLACEMENT/FLAGS' filename

学到了如何将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 'ADDERSSw outputfile' inputfilename
#sed '/PATTERN/w outputfile' inputfilename

学到了同时执行多段脚本:

sed -n -e '1w output.txt' -e '$w output.txt' thegeekstuff.txt

如何执行多个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可写可不写。(难怪)
删除第一行, 最后一行和空白行

sed -e '1d' -e '$d' -e '/^$/d' thegeekstuff.txt

高级替换

6-1如果REGEX或REPLACEMENT中有/,则需要用\转义,或者使用@ % | ; : 做为sed的分隔符

sed 's@/opt/omni/lbin@/opt/tools/bin@g' path.txt

以上用@做为分隔符替换某个路径值

6-2 &用在REPLACEMENT指代REGEX实际匹配的那部分内容

sed 's@/usr/bin@&/local@g' path.txt

这里的&指代/usr/bin, 又比如

sed 's@^.*$@<<<&>>>@g' path.txt

把每行用<<<>>>括起来。。这个用法比较实用

6-3分组和back refercence
A group is opened with “(” and closed with “)”, 并且back reference即可用在REGEX部分也可以用在replacement部分

sed 's/\(\/[^:]*\).*/\1/g' path.txt

每行只保留第一个冒号前面的路径
==> /usr/kbos/bin:/usr/local/bin:/usr/jbin/ 变成 /usr/kbos/bin

又比如:

$ sed '$s@\([^:]*\):\([^:]*\):\([^:]*\)@\3:\2:\1@g' path.txt

颠倒最后一行3个路径的出现顺序,

又比如

sed 's/\([^:]*\).*/\1/' /etc/passwd

从/etc/passwd中取所有的用户名(第一列就是)

又比如

$ echo "Welcome To The Geek Stuff" | sed 's/\(\b[A-Z]\)/\(\1\)/g'
(W)elcome (T)o (T)he (G)eek (S)tuff

将每个单词首字母用括号括起来,我试了一下,这个例子中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

#sed 'ADDRESS a\
    Line which you want to append' filename

#sed '/PATTERN/ a\
    Line which you want to append' filename
在上面的syntax中, a表示在某行之后插入, 之前用i, 替换用c

比如我有一个文件hello.sh内容如下:
echo "hello cyper"

使用后插a,前插i,和替换c之后的效果如下:

$ sed '1 a#!/bin/sh' hello.sh
echo "hello cyper"
#!/bin/sh
$ sed '1 i#!/bin/sh' hello.sh
#!/bin/sh
echo "hello cyper"
$ sed '1 c#!/bin/sh' hello.sh
#!/bin/sh
$ 

当然我的本意是前插, 所以在这种情况下选择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/

@uniquejava
Copy link
Owner Author

mac

mac中的所有sed -i 'command' filename要改成sed -i '' 'command' filename
也就是说-i后面多了个空字符串,这是因为mac中的sed不是gnu sed
使用brew install gnu-sed可以安装,然后使用gsed。。

@uniquejava
Copy link
Owner Author

示例一:

#!/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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant