- regular expressions
Regular solution: #Conform to IP basic format screening Basic
regular: grep
Extended regular: egrep
- character qualifier class
- Dot means match any character -----------> abc. can match abc9 abcd
- [ ] can match any one in [ ]d ----> [abc]t can match at, bt, ct
- – range of matching strings ---------------> [0-9]
- [[:xxx:]]grep some predefined command characters, [[:alpha:]] matches a letter, [[:digit:]] matches a number
- ^ is located at the beginning of the brackets [ ], matching any character other than the characters in the brackets,
so [ ^xy ] matches any character except xy [^xy]o can match ao, bo...
2. Quantity qualifiers
1. ?:表示紧跟在他前面单元应该匹配零次或者一次
[0-9]?\.[0-9]匹配0,2.3,等,因为.在正则表达式中有自己的含义因此需要用\转义一下筛选出小数点
2. +:紧跟在他前面的单元匹配一次或者多次
3. *:表示紧跟在前面匹配0次或者多次
4.(n)
-sed usage
//在第三行后面添加hello
sed '3ahello' 1.txt;
//在有123后面添加hello
sed '/123/ahello' 1.txt;
//文本最后一行添加hello
sed '$ahello' 1.txt
//在第三行之前插入hello
sed '3ihello' 1.txt;
sed '/123/ihello/' 1.txt;
sed '$ihello' 1.txt
//将第一行更改成为hello;
sed '1chello' 1.txt
//将包含123的内容更改成为hello
sed '/123/chello' 1.txt
//将最后一行替换成为hello
sed '$chello' 1.txt
//删除第四行内容
sed '4d' 1.txt
//从第一行开始删除,每隔2行就删除一行
sed '1~2d' 1.txt
//删除1-2行
sed '1,2d' 1.txt
//删除1-2行之外的所有行
sed '1,2!d' 1.txt
//删除最后一行
sed '$d' 1.txt
//删除包含123的行
sed '/123/d' 1.txt
//删除包含123的所有行
sed '/123/,d' 1.txt
//删除空行
sed '/^$/d' 1.txt
//删除不匹配123或者abc的行
sed '/123\|abc/!d' 1.txt
//删除1-3行中匹配123或abc的行
sed '1,3{/123\|abc/d}' 1.txt
//将文件中的123替换成hello默认之替换第一个
sed 's/123/hello/' 1.txt
//文本中所有都替换
sed 's/123/hello/g' 1.txt
//将每行中第二个123替换
sed 's/123/hello/2' 1.txt
//将每一行中匹配的123替换成hello
//并且将替换内容写入2.txt中
sed -n 's/123/hello/gpw 2.txt' 1.txt
//匹配所有的#的行,替换行中逗号的所有内容为空,(,.*)
//表示逗号后面所有内容为空
sed '/#/s/,.*//g' 1.txt
//将最后一行的两个字符替换为空
sed 's/..$//g' 1.txt
//匹配#号后面所有的内容为空
sed 's/^#.*//g' 1.txt
//先替换1.txt文件中所有注释行为空行
//然后删除空行,删除和替换用;分隔开来
sed 's/^#.*//;/^$/d' 1.txt
//在每一行后面加上“hahah"
sed 's/$/&'haha'/' 1.txt
----------打印问价中的行-------------------
//打印文件中第三行
sed -n '3p' 1.txt
//从第二行开始每隔离两行打印一行
sed -n '2~2p' 1.txt
//打印最后一行
sed -n '$p' 1.txt
//打印1-3行
sed -n '1,3p' 1.txt
//打印从匹配too行到最后一行的内容
sed -n '/too/,+1p' 1.txt
//打印从boo到too内容
sed -n '/boo/,/too/' 1.txt
//打印文件的行号
sed -n '$=' 1.txt
//打印匹配到error的行号
sed -n '/eror/=' 1.txt
//打印匹配到error的行号和内容
sed -n '/error/{=;p}' 1.txt
//将文件内容读取出来读入1.txt
sed 'r 2.txt' 1.txt
//在1.txt第三行之后插入2.txt 的文件内容
sed '3r 2.txt' 1.txt
//在1.txt 中123 内容后面插入2.txt的文件内容
sed '/245/r 2.txt' 1.txt
-----------------向文件中写入内容--------------------------
//将1.txt中第二行的内容写入文件中
sed -n '2w 2.txt' 1.txt
//将1.txt的第一行和最后一行分别写入1.txt和3.txt中
sed -n -e '1w 2.txt' -e '$w 3.txt' 1.txt
-----------------sed在shell脚本中的使用--------------------------
#替换文件内容
#!/bin/bash
if [ $# -ne 3 ];then
echo "Usage: $0 oldpart new-part filenam"
return
fi
sed -i "s#$1#$2#" $3
#删除文件中的空白行
#!/bin/bash
if [ ! -f $1];then
echo "$0 is not a file"
exit
fi
sed -i "/^$/d" $1
#批量更改当前目录中的文件后缀名字
#!/bin/bash
for i in [$1]; do
if [ -f $i ]; then
iname=`basename $i` #获取文件名
newname=`echo $iname | sed -e "s/$1/$2/g"`
mv $iname $newname
fi
done