AIX下系统sed使用详解

来源:互联网 发布:淘宝店铺装修定时发布 编辑:程序博客网 时间:2024/05/24 06:16
使用sed去修改或者删除文本中的字符或者字符串。pg func.txt0at$the@begining^MThe#file#name#is#func,^M9and%it's%suffix%is .txt1.查找包含"#"的行:awk '$0 ~ /#/' func.txtThe#file#name#is#func,^M2.将包含"#"的行中第一个"#"替换为空格:sed -n 's/#/ /p' func.txtThe file#name#is#func,^M3.替换行中所有的"#":sed 's/#/ /g' func.txt0at$the@begining^MThe file name is func,^M9and%it's%suffix%is .txt4.替换行开头的数字:sed 's/^[0-9]*//g' func.txtat$the@begining^MThe#file#name#is#func,^Mand%it's%suffix%is .txt5.将结尾的^M去掉:sed 's/^M$//g' func.txt0at$the@begining^MThe#file#name#is#func,^M9and%it's%suffix%is .txt怎么没替换呢?原来^为特殊字符,需要转义sed 's/\^M$//g' func.txt0at$the@beginingThe#file#name#is#func,9and%it's%suffix%is .txt6.下面将这些命令全部整合起来:pg func.txt0at$the@begining^MThe#file#name#is#func,^M9and%it's%suffix%is .txtat func.txt | sed 's/\$/ /g' | sed 's/@/ /g' | se 's/^[0-9]//g' | sed 's/\^M$//g' | sed 's/#/ /g' | sed 's/%/ /g'at the beginingThe file name is func,and it's suffix is .txt也可以将这些命令放在文件里面:pg func.sed# !/bin/sed -f# drop the "#"s/#/ /g# drop the number at the first of each lines/^[0-9]//g# drop the "$"s/\$/ /g# drop the "@"s/@/ /g# drop the "%"s/%/ /g# drop the "^M"s/\^M//g# EOF执行命令:sed -f func.sed func.txtat the beginingThe file name is func,and it's suffix is .txt将执行过滤后的结果保存到sed.out文件中:sed -f func.sed func.txt > sed.outpg sed.outat the beginingThe file name is func,and it's suffix is .txt下面一个适用的例子我从数据库中查找的数据放在一个文件里面:pg sql.txtLASTNAME        SALARY--------------- -----------HAAS              152750.00THOMPSON           94250.00  2 条记录已选择。现在的需求是将其中的LASTNAME取出来,可以如下操作:cat sql.txt | sed '/^--*/d' | sed '/^$/d' | sed '$d' | sed '1d' | awk '{print $1}'取出其中的数字:cat sql.txt | sed '1d' | sed '$d' | sed '/^$/d' | sed '/^--*/d' | awk '{print $2}'152750.0094250.00在每行后面附加信息pg info.txtyeeXunLinuxAixUnixWindowssed 's/[a-zA-Z]*/& -end-/g' info.txtyeeXun -end-Linux -end-Aix -end-Unix -end-Windows -end-在命令行给sed传递值,使用双引号:NAME="Scott in Oracle"REPLACE="OUT"echo $NAME | sed "s/in/$REPLACE/g"Scott OUT Oracle下面是一些行命令([]表示空格,[ ] 表示tab键)-------------------------------------------------------------------'s/\.$//g'删除以.结尾的行'-e /abcd/d'删除包含abcd的行's/[][][]*/[]/g'用一个空格替换多个空格's/^[][]*//g'删除行首空格's/\.[][]*/[]/g'用一个空格替换.后面的多个空格'/^$/d'删除空行's/^.//g'删除行首的第一个字符's/COL\(...\)//g'删除紧跟COL(的三个字符's/^\///g'从路劲中删除第一个\'s/[ ]/[]//g'用空格替代tab键's/^[ ]//g'删除行首所有tab键's/[ ]*//g'删除所有tab键-------------------------------------------------------------------脚本集合1.删除路径名第一个\:echo $PWD | sed 's/^\///g'usr/xxxx/ytcclb/sed2.附加(添加)文本:echo "Mac Wong" | sed 's/Mac /& J./g'Mac  J.Wong3.取文件名,去掉后缀:查看当前目录下的文件:ls -ltotal 20-rwxr--r--   1 b4nx     group         78 Dec  4 09:48 append.sed-rw-r--r--   1 b4nx     group         48 Dec  4 10:01 change.sed-rw-r--r--   1 b4nx     group        181 Dec  6 10:41 func.sed-rw-r--r--   1 b4nx     group         69 Dec  6 09:58 func.txt-rw-r--r--   1 b4nx     group         30 Dec  8 13:57 info.txt-rw-r--r--   1 b4nx     group         44 Dec  4 09:56 insert.sed-rw-r--r--   1 b4nx     group        201 Nov 27 15:01 quote.txt-rw-r--r--   1 b4nx     group         63 Dec  6 10:43 sed.out-rw-r--r--   1 b4nx     group          5 Dec  4 14:43 sedex.txt-rw-r--r--   1 b4nx     group        125 Dec  6 10:55 sql.txt取文件名:ls -l | awk '{print $9}' | sed '/^$/d' | sed 's/\....//g'appendchangefuncfuncinfoinsertquotesedsedexsql4.给3取出来的文件添加后缀.db2:ls -l | awk '{print $9}' | sed '/^$/d' | sed 's/\..*$//g' | sed 's/$/.db2/g'append.db2change.db2func.db2func.db2info.db2insert.db2quote.db2sed.db2sedex.db2sql.db2注意:取文件的后缀:sed 's/\..*$//g'5.替换多个字符(包括空格):str="Guiyang&is thecapital of GuiZhou"echo $str | sed 's/&/ /g' | sed 's/the/the /g'Guiyang is the capital of GuiZhou
--the end--
原创粉丝点击