sed 例子--很不错的。
时间:2010-07-26 来源:huanghaojie
http://apps.hi.baidu.com/share/detail/9157472
一些shell脚本,sed替换 一,将SVN版本信息添加到我们的程序版本中
1,使用sed命令修改源代码/Makefile中的版本号,添加SVN版本信息:
#!/bin/sh
#File Name: build-l200.sh
#Get the SVN version number
svn_ver=`svn up|grep revision|awk -F' ' '{ print $3 }'|awk -F'.' '{print $1}'`
echo "SVN Version: $svn_ver"
#Find the Version line
line=`sed -n '/#define DROPBEAR_VERSION/=' options.h`
#Instead the original version by mine
sed -i -e ${line}s"/.*/#define DROPBEAR_VERSION \"0.51 Build ${svn_ver}\"/" options.h
#compile the source code
make
2, 将SVN版本信息放入version.h头文件中,在源代码中在包含该头文件并引用他们:
a, makefile中定义版本信息并调用scripts/mkversion脚本:
CROSS_COMPILE =
ARCH= L200
MAJOR=1
MINOR=0
REVER=0
SVNUP=1
all: entry version at
version:
@echo " ";
@echo " =========================================";
@echo " ** Create Software Version Head File **";
@echo " =========================================";
sh scripts/mkversion ${SVNUP} ${MAJOR} ${MINOR} ${REVER}
b, scripts/mkversion脚本:
#!/bin/sh
GET_SVN=$1
MAJOR=$2
MINOR=$3
REVER=$4
if [ $GET_SVN -eq 0 ]
then
echo "============ Skip svn update ============"
exit;
else
echo "============ SVN update and get svn version now ============"
svn up
svn_ver=`svn up|grep revision|awk -F' ' '{ print $3 }'|awk -F'.' '{print $1}'`
echo "/* This is generate by makefile , don't Edit it by hand */" > version.h
echo "#define MAJOR ${MAJOR}" >>version.h
echo "#define MINOR ${MINOR}" >>version.h
echo "#define REVER ${REVER}" >>version.h
if [ ! -z $svn_ver ]
then
echo "#define SVNVER $svn_ver" >>version.h
else
echo "#define SVNVER 0000" >>version.h
fi
fi
3, 使用sed修改Makefile文件: 替换行/行前插入/行后插入
例一:
FILE=src/pkcs11/Makefile
#将文件中所有的$KEY替换成$INSTEAD
KEY="-module -shared -avoid-version -no-undefined"
INSTEAD="-module -static -avoid-version -no-undefined"
sed -i -e "s/${KEY}/$INSTEAD/g" $FILE
#找到某一行,并在其后插入一些行;
FILE=src/libopensc/Makefile
line=`sed -n '/all: all-am/=' $FILE`
#替换该行内容
sed -i -e ${line}s"/.*/all: all-am L200_rebuildLib/" $FILE
#在该行后插入一些新的行
sed -i -e `expr $line + 1`a"L200_rebuildLib:" $FILE
sed -i -e `expr $line + 2`a"\\\tcp \`dirname \${abs_top_srcdir}\`/pcsc-lite-1.5.5/src/.libs/libpcscl
ite.a \${abs_srcdir}/.libs" $FILE
sed -i -e `expr $line + 3`a"\\\tsh \${abs_srcdir}/L200_rebuildLib.sh \${abs_srcdir}\n" $FILE
找到某一行,在该行前插入一行:
FILE=src/Makefile
#找到要修改的行号
line=`sed -n '/CFLAGS = -Wall -fno-common/=' $FILE`
#替换该行的内容
sed -i -e ${line}s"/.*/CFLAGS = -Wall -fno-common -O2 -Wl,-elf2flt='-s 262144'/" $FILE
#在该行前插入一行注释
sed -i -e ${line}i"# Set stack size as 256K" $FILE
找到某一行,删除该行第一个字符:
KEY="#am__EXEEXT_1 = cryptoflex-tool"
line=`sed -n "/$KEY/=" $FILE`
if [ $line ] ; then #如果变量不为空,即找到该行
sed -i -e "${line}s/.//1" $FILE #这里的1即为第一个字符,若修改第二个字符则改为2
sed -i -e "`expr $line + 1`s/.//1" $FILE
sed -i -e "`expr $line + 2`s/.//1" $FILE
fi
例二:
CROSS=/opt/buildroot_350/build_arm/staging_dir/bin/arm-linux-uclibc-
# | -----------------------------
# | Cross compile iproute2-2.6.34
# | -----------------------------
if [ ! -d iproute2-2.6.34 ] ; then
tar -xjf iproute2-2.6.34.tar.bz2
fi
if [ -d iproute2-2.6.34 ]; then
cd iproute2-2.6.34
FILE=Makefile
sed -i -e 38s"/.*/SUBDIRS=lib tc/" $FILE
line=`sed -n '/^CC = gcc$/=' $FILE`
if [ $line ]; then
sed -i -e ${line}s"@.*@CROSS=$CROSS@" $FILE //由于$CROSS中中包含sed默认分隔符"\",所以我们这里改用@作为分隔符
sed -i -e `expr $line + 0`a"CC = \${CROSS}gcc" $FILE
sed -i -e `expr $line + 1`a"AR=\${CROSS}ar" $FILE
sed -i -e `expr $line + 2`a"AS=\${CROSS}as" $FILE
sed -i -e `expr $line + 3`a"LD=\${CROSS}ld" $FILE
sed -i -e `expr $line + 4`a"RANLIB=\${CROSS}ranlib" $FILE
fi
make && cd -
fi
# | -----------------------------
# | Cross compile redir-2.2.1
# | -----------------------------
if [ ! -d redir-2.2.1 ] ; then
tar -xzf redir-2.2.1.tar.gz
fi
if [ -d redir-2.2.1 ]; then
cd redir-2.2.1
FILE=Makefile
sed -i -e s":^CC = gcc$:CC = ${CROSS}gcc:" $FILE //这里选用":"作为sed 分隔符
make && cd -
fi
二,一些常用的alias:
alias vt100='export TERM=vt100'
alias linux='export TERM=linux'
alias maek='make'
alias rmsvn='find -name .svn -exec rm -rf {} \;'
alias rmgdb='find -iname "*.gdb" -exec rm -rf {} \;'
alias rmdep='find -name .depend -exec rm -rf {} \;'
alias myindent='indent -npro -kr -bl -nce -bli0 -i2 -ts2 -sob -l80 -nfc1 -ss -ncs'
alias myctages='ctags --c-kinds=+defglmnstuvx --langmap=c:.c.h.ho.hem.het.hec.hev.him.hit.hic.hiv -R .'
alias nocvim='mv ~/.vimrc ~/.vimrcb'
alias cvim='mv ~/.vimrcb ~/.vimrc'
alias mntwinxp='sudo mount -t cifs -o username="username",password="password" //Winxp_ipaddr/linux /home/username/winxp/'
用于在文件中查找的脚本:
#!/bin/sh
# Description: This shell script used to grep the "key" in all the files
find -iname "*$suffix" | xargs grep "$key"
find -iname "*$suffix" | xargs grep "\<$key\>"
# in current folder.
# Author: guowenxue([email protected])
# Version: 1.0.0 (Release by guowenxue on 04th Feb. 2010)
# ChangeLog: None
#
if [ $# -lt 1 ] ; then
echo " Usage: $0 key [-g] [suffix]"
echo "Example1: mygrep haha"
echo "Example2: mygrep haha -g"
echo "Example1: mygrep haha *.c"
echo "Example1: mygrep haha -g *.c"
exit;
fi
key=$1 #The grep key
if [ $# = 1 ] ; then
grep -n "$key" -r *
elif [ $# = 2 ] ; then
if [ $2 = "-g" ] ; then
grep -n "\<$key\>" -r *
else
suffix=$2
find -iname "$suffix" | xargs grep -n "$key"
fi
else # Arguments more than 3
if [ $2 = "-g" ] ; then
suffix=$3
elif [ $3 = "-g" ]; then
suffix=$2
fi
find -iname "$suffix" | xargs grep -n "\<$key\>"
fi
[guowenxue@localhost drivers]$ mygrep 15000400
csps/lpc313x/include/lpc313x_chip.h:#define LCD_INTERFACE_BASE 0x15000400
[guowenxue@localhost drivers]$ mygrep 15000400 .h
./csps/lpc313x/include/lpc313x_chip.h:#define LCD_INTERFACE_BASE 0x15000400
[guowenxue@localhost drivers]$ mygrep 15000400 .h -g
[guowenxue@localhost drivers]$ mygrep 15000400 -g .h
Sed命令去掉补丁文件中首字符"+",获取C代码文件:
[guowenxue@localhost spi]$ sed -i 's/^+//' tsc2046.h
一些shell脚本,sed替换 一,将SVN版本信息添加到我们的程序版本中
1,使用sed命令修改源代码/Makefile中的版本号,添加SVN版本信息:
#!/bin/sh
#File Name: build-l200.sh
#Get the SVN version number
svn_ver=`svn up|grep revision|awk -F' ' '{ print $3 }'|awk -F'.' '{print $1}'`
echo "SVN Version: $svn_ver"
#Find the Version line
line=`sed -n '/#define DROPBEAR_VERSION/=' options.h`
#Instead the original version by mine
sed -i -e ${line}s"/.*/#define DROPBEAR_VERSION \"0.51 Build ${svn_ver}\"/" options.h
#compile the source code
make
2, 将SVN版本信息放入version.h头文件中,在源代码中在包含该头文件并引用他们:
a, makefile中定义版本信息并调用scripts/mkversion脚本:
CROSS_COMPILE =
ARCH= L200
MAJOR=1
MINOR=0
REVER=0
SVNUP=1
all: entry version at
version:
@echo " ";
@echo " =========================================";
@echo " ** Create Software Version Head File **";
@echo " =========================================";
sh scripts/mkversion ${SVNUP} ${MAJOR} ${MINOR} ${REVER}
b, scripts/mkversion脚本:
#!/bin/sh
GET_SVN=$1
MAJOR=$2
MINOR=$3
REVER=$4
if [ $GET_SVN -eq 0 ]
then
echo "============ Skip svn update ============"
exit;
else
echo "============ SVN update and get svn version now ============"
svn up
svn_ver=`svn up|grep revision|awk -F' ' '{ print $3 }'|awk -F'.' '{print $1}'`
echo "/* This is generate by makefile , don't Edit it by hand */" > version.h
echo "#define MAJOR ${MAJOR}" >>version.h
echo "#define MINOR ${MINOR}" >>version.h
echo "#define REVER ${REVER}" >>version.h
if [ ! -z $svn_ver ]
then
echo "#define SVNVER $svn_ver" >>version.h
else
echo "#define SVNVER 0000" >>version.h
fi
fi
3, 使用sed修改Makefile文件: 替换行/行前插入/行后插入
例一:
FILE=src/pkcs11/Makefile
#将文件中所有的$KEY替换成$INSTEAD
KEY="-module -shared -avoid-version -no-undefined"
INSTEAD="-module -static -avoid-version -no-undefined"
sed -i -e "s/${KEY}/$INSTEAD/g" $FILE
#找到某一行,并在其后插入一些行;
FILE=src/libopensc/Makefile
line=`sed -n '/all: all-am/=' $FILE`
#替换该行内容
sed -i -e ${line}s"/.*/all: all-am L200_rebuildLib/" $FILE
#在该行后插入一些新的行
sed -i -e `expr $line + 1`a"L200_rebuildLib:" $FILE
sed -i -e `expr $line + 2`a"\\\tcp \`dirname \${abs_top_srcdir}\`/pcsc-lite-1.5.5/src/.libs/libpcscl
ite.a \${abs_srcdir}/.libs" $FILE
sed -i -e `expr $line + 3`a"\\\tsh \${abs_srcdir}/L200_rebuildLib.sh \${abs_srcdir}\n" $FILE
找到某一行,在该行前插入一行:
FILE=src/Makefile
#找到要修改的行号
line=`sed -n '/CFLAGS = -Wall -fno-common/=' $FILE`
#替换该行的内容
sed -i -e ${line}s"/.*/CFLAGS = -Wall -fno-common -O2 -Wl,-elf2flt='-s 262144'/" $FILE
#在该行前插入一行注释
sed -i -e ${line}i"# Set stack size as 256K" $FILE
找到某一行,删除该行第一个字符:
KEY="#am__EXEEXT_1 = cryptoflex-tool"
line=`sed -n "/$KEY/=" $FILE`
if [ $line ] ; then #如果变量不为空,即找到该行
sed -i -e "${line}s/.//1" $FILE #这里的1即为第一个字符,若修改第二个字符则改为2
sed -i -e "`expr $line + 1`s/.//1" $FILE
sed -i -e "`expr $line + 2`s/.//1" $FILE
fi
例二:
CROSS=/opt/buildroot_350/build_arm/staging_dir/bin/arm-linux-uclibc-
# | -----------------------------
# | Cross compile iproute2-2.6.34
# | -----------------------------
if [ ! -d iproute2-2.6.34 ] ; then
tar -xjf iproute2-2.6.34.tar.bz2
fi
if [ -d iproute2-2.6.34 ]; then
cd iproute2-2.6.34
FILE=Makefile
sed -i -e 38s"/.*/SUBDIRS=lib tc/" $FILE
line=`sed -n '/^CC = gcc$/=' $FILE`
if [ $line ]; then
sed -i -e ${line}s"@.*@CROSS=$CROSS@" $FILE //由于$CROSS中中包含sed默认分隔符"\",所以我们这里改用@作为分隔符
sed -i -e `expr $line + 0`a"CC = \${CROSS}gcc" $FILE
sed -i -e `expr $line + 1`a"AR=\${CROSS}ar" $FILE
sed -i -e `expr $line + 2`a"AS=\${CROSS}as" $FILE
sed -i -e `expr $line + 3`a"LD=\${CROSS}ld" $FILE
sed -i -e `expr $line + 4`a"RANLIB=\${CROSS}ranlib" $FILE
fi
make && cd -
fi
# | -----------------------------
# | Cross compile redir-2.2.1
# | -----------------------------
if [ ! -d redir-2.2.1 ] ; then
tar -xzf redir-2.2.1.tar.gz
fi
if [ -d redir-2.2.1 ]; then
cd redir-2.2.1
FILE=Makefile
sed -i -e s":^CC = gcc$:CC = ${CROSS}gcc:" $FILE //这里选用":"作为sed 分隔符
make && cd -
fi
二,一些常用的alias:
alias vt100='export TERM=vt100'
alias linux='export TERM=linux'
alias maek='make'
alias rmsvn='find -name .svn -exec rm -rf {} \;'
alias rmgdb='find -iname "*.gdb" -exec rm -rf {} \;'
alias rmdep='find -name .depend -exec rm -rf {} \;'
alias myindent='indent -npro -kr -bl -nce -bli0 -i2 -ts2 -sob -l80 -nfc1 -ss -ncs'
alias myctages='ctags --c-kinds=+defglmnstuvx --langmap=c:.c.h.ho.hem.het.hec.hev.him.hit.hic.hiv -R .'
alias nocvim='mv ~/.vimrc ~/.vimrcb'
alias cvim='mv ~/.vimrcb ~/.vimrc'
alias mntwinxp='sudo mount -t cifs -o username="username",password="password" //Winxp_ipaddr/linux /home/username/winxp/'
用于在文件中查找的脚本:
#!/bin/sh
# Description: This shell script used to grep the "key" in all the files
find -iname "*$suffix" | xargs grep "$key"
find -iname "*$suffix" | xargs grep "\<$key\>"
# in current folder.
# Author: guowenxue([email protected])
# Version: 1.0.0 (Release by guowenxue on 04th Feb. 2010)
# ChangeLog: None
#
if [ $# -lt 1 ] ; then
echo " Usage: $0 key [-g] [suffix]"
echo "Example1: mygrep haha"
echo "Example2: mygrep haha -g"
echo "Example1: mygrep haha *.c"
echo "Example1: mygrep haha -g *.c"
exit;
fi
key=$1 #The grep key
if [ $# = 1 ] ; then
grep -n "$key" -r *
elif [ $# = 2 ] ; then
if [ $2 = "-g" ] ; then
grep -n "\<$key\>" -r *
else
suffix=$2
find -iname "$suffix" | xargs grep -n "$key"
fi
else # Arguments more than 3
if [ $2 = "-g" ] ; then
suffix=$3
elif [ $3 = "-g" ]; then
suffix=$2
fi
find -iname "$suffix" | xargs grep -n "\<$key\>"
fi
[guowenxue@localhost drivers]$ mygrep 15000400
csps/lpc313x/include/lpc313x_chip.h:#define LCD_INTERFACE_BASE 0x15000400
[guowenxue@localhost drivers]$ mygrep 15000400 .h
./csps/lpc313x/include/lpc313x_chip.h:#define LCD_INTERFACE_BASE 0x15000400
[guowenxue@localhost drivers]$ mygrep 15000400 .h -g
[guowenxue@localhost drivers]$ mygrep 15000400 -g .h
Sed命令去掉补丁文件中首字符"+",获取C代码文件:
[guowenxue@localhost spi]$ sed -i 's/^+//' tsc2046.h
相关阅读 更多 +