shell+Day2

- 正则表达式

正则解决:#符合IP基本格式筛选
基础正则: grep
扩展正则:egrep

  1. 字符限定符类
  1. 点· 表示匹配任意一个字符 -----------> abc. 可以匹配 abc9 abcd
  2. [ ]可以匹配[ ]d里面的任意一个----> [abc]t 可以匹配at,bt,ct
  3. – 匹配字符串的范围 ---------------> [0-9]
  4. [[:xxx:]]grep预定义的一些命令字符,[[:alpha:]]匹配一个字母,[[:digit:]]匹配一个数字
  5. ^位于[ ]括号内的开头 ,匹配括号中字符以外的任意一个字符
    因此[ ^xy ]匹配除了xy以外的任一个字符[^xy]o可以匹配ao,bo…

2.数量限定符

1. ?:表示紧跟在他前面单元应该匹配零次或者一次
	[0-9]?\.[0-9]匹配0,2.3,等,因为.在正则表达式中有自己的含义因此需要用\转义一下筛选出小数点
2. +:紧跟在他前面的单元匹配一次或者多次
3. *:表示紧跟在前面匹配0次或者多次
4.(n)

-sed用法

	//在第三行后面添加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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值