1>.Sed 读取数据
sed
A).使用sed
从一行开始查找一直到出现有某个词语的行结束
The honesuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendanc.
-->
//从第二行 到出现The的行
sed -n '2,/The/'p test.txt
B). 匹对字符
查找一个含有 $的字符的行
sed -n '/\$/'p test.txt
The honesuckle band played all night long for only $90.
-->
sed -n '/The/'p test.txt
The honesuckle band played all night long for only $90.
The local nurse Miss P.Neave was in attendanc.
-->
C).整篇显示
sed -n '1,$p' test.txt
D).任何单词的行
sed -n '/.*ing/'p test.txt
E).显示行号
sed -n -e '/music/=' test.txt
如果既显示行号又显示该行的内容可以使用
sed -n -e '/music/p' -e '/music/=' test.txt
F). 第一行与最后一行的显示
# the first line
sed -n '1p' test.txt
# and the last line
sed -n '$p' test.txt
2>.sed 的 添加字符和文件
3>.sed的替换与删除
The honesuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendanc.
-->
A).首字符的删除
#删除每行开始的T字母
sed 's/^T*//g' test.txt
#把经过输出到一个新的文件test1.txt中
sed 's/^T*//gw test1.txt' test.txt
w --文件名 将结果输出定向到一个文件
p --缺省情况下将被替换行写入标准输出,
n --不打印输出结果.
-->
关于 's///' 命令的另一个妙处是 '/' 分隔符有许多替换选项。如果正在执行字符串替换,并且规则表达式或替换字符串中有许多斜杠,则可以通过在 's' 之后指定一个不同的字符来更改分隔符。例如,下例将把所有出现的
/usr/local 替换成 /usr:
$ sed -e 's:/usr/local:/usr:g' mylist.txt
加入变量FIRMWARE_V=404.000.00.120800,如果要把里面的404.000.00.120801替换为变量值,则在变量中使用"''"来替换
sed -i 's/firmware_versions = "404.000.00.120801"/firmware_versions = "'$FIRMWARE_V'"/g'
lichee/tools/pack/chips/sun5i/configs/android/U1S/sys_config1.fex
在第一行上插入TARGET_DEVICE:=Superbox_Smart_box-1
sed -r -i '1 i TARGET_DEVICE:=Superbox_Smart_box-1' build/core/Makefile
反斜杠\在替换里,跟在錨點前面表示该錨點只是表达式里面的符号,而不是錨點。例如,下例将把所有出现的 /usr/local 替换成 /usr
sed -r -i 's/\/usr\/local/\/usr/g' mylist.txt
\\ 第一个\是后一个\的转译
'\'' 在shell下在单引号内的单引号转译需要用'\''[[:digit:]]\{16\} 任意的16位数字