AIX下系统sed使用详解

使用sed去修改或者删除文本中的字符或者字符串。 pg func.txt 0at$the@begining^M The#file#name#is#func,^M 9and%it’s%suffix%is .txt 1.查找包含”#”的行: awk ‘$0 ~ /#/’ func.txt The#file#name#is#func,^M 2.将包含”#”的行中第一个”#”替换为空格: sed -n ‘s/#/ /p’ func.txt The file#name#is#func,^M 3.替换行中所有的”#”: sed ‘s/#/ /g’ func.txt 0at$the@begining^M The file name is func,^M 9and%it’s%suffix%is .txt 4.替换行开头的数字: sed ‘s/^[0-9]*//g’ func.txt at$the@begining^M The#file#name#is#func,^M and%it’s%suffix%is .txt 5.将结尾的^M去掉: sed ‘s/^M$//g’ func.txt 0at$the@begining^M The#file#name#is#func,^M 9and%it’s%suffix%is .txt 怎么没替换呢? 原来^为特殊字符,需要转义 sed ‘s/\^M$//g’ func.txt 0at$the@begining The#file#name#is#func, 9and%it’s%suffix%is .txt 6.下面将这些命令全部整合起来: pg func.txt 0at$the@begining^M The#file#name#is#func,^M 9and%it’s%suffix%is .txt at 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 begining The 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 line s/^[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.txt at the begining The file name is func, and it’s suffix is .txt 将执行过滤后的结果保存到sed.out文件中: sed -f func.sed func.txt > sed.out pg sed.out at the begining The file name is func, and it’s suffix is .txt 下面一个适用的例子 我从数据库中查找的数据放在一个文件里面: pg sql.txt LASTNAME SALARY ————— ———– HAAS 152750.00 THOMPSON 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.00 94250.00 在每行后面附加信息 pg info.txt yeeXun Linux Aix Unix Windows sed ‘s/[a-zA-Z]*/& -end-/g’ info.txt yeeXun -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键 ——————————————————————-

再发展下来才有了:大霞美的花卉基地和清源山的花博园。

AIX下系统sed使用详解

相关文章:

你感兴趣的文章:

标签云: