AIX上系统sed使用详解
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键 ------------------------------- 脚本集合 1.删除路径名第一个\: echo $PWD | sed 's/^\///g' usr/xxxx/ytcclb/sed 2.附加(添加)文本: echo "Mac Wong" | sed 's/Mac /& J./g' Mac J.Wong 3.取文件名,去掉后缀: 查看当前目录下的文件: ls -l total 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' append change func func info insert quote sed sedex sql 4.给3取出来的文件添加后缀.db2: ls -l | awk '{print $9}' | sed '/^$/d' | sed 's/\..*$//g' | sed 's/$/.db2/g' append.db2 change.db2 func.db2 func.db2 info.db2 insert.db2 quote.db2 sed.db2 sedex.db2 sql.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--