Linux基础知识:Sed单行处理与多行处理格式

本文介绍了Linux中的Sed命令,包括元字符的概念,Sed的单行处理和多行处理模式,以及如何使用Sed进行文本替换和操作。对于Linux运维人员和系统管理员来说,掌握Sed的用法对于日常文本处理任务非常实用。

 1、元字符介绍

.  匹配除换行符外的任意单个字符
*  匹配任意一个跟在它前面的字符
[] 匹配方括号中的字符类中的任意一个
^  匹配开头
$  匹配结尾
\  转义后面的特殊字符

+ 匹配前面的正则表达式至少出现一次
?匹配前面的正则表达式出现零次或一次
|  匹配它前面或后面的正则表达式


-----------------------

举例说明.和$,借助文本查找工具grep

[root@harbor ~]# grep password /root/anaconda-ks.cfg
# Root password

[root@harbor ~]# grep pass... /root/anaconda-ks.cfg
auth --enableshadow --passalgo=sha512
# Root password
You have new mail in /var/spool/mail/root

[root@harbor ~]# grep pass....$ /root/anaconda-ks.cfg
# Root password

[root@harbor ~]# grep  pass* /root/anaconda-ks.cfg
auth --enableshadow --passalgo=sha512
# Root password[root@harbor ~]# 

[root@harbor ~]# grep  ^#  /root/anaconda-ks.cfg
#version=DEVEL
# System authorization information
# Use CDROM installation media
# Use graphical install
# Run the Setup Agent on first boot
# Keyboard layouts
# System language
# Network information
# Root password
# System services
# System timezone
# X Window System configuration information
# System bootloader configuration
# Partition clearing information
# Disk partitioning information

2、Find演示

查找文件

find /etc -name pass*

使用正则表达式

find /etc -regex  .*wd


atime  ---最后一次访问文件(读取或执行)的时间

ctime  ---最后一次改变文件(属性)或者目录(属性)的时间

mtime ---最后一次改变文件(内容)或目录(内容)的时间

find /etc/ -ctime 8

LANG=C stat filea 用英文状态显示文件的Atime Ctime Mtime

grep pass /root/anaconda-ks.cfg | cut -d " " -f 1
表示在/root/这个目录下过滤出pass这个字段,传递给cut命令,cut使用空格分隔符,取第一个字段


cut -d 字段分隔符,这里“”代表的是用空格做为分隔符
      -f 仅选择这些字段;也打印不包含分隔符的任何行,除非指定了-s选

3、Sed 单行处理模式

sed 行编辑器

sed 一般用于对文本内容做替换

	sed ’/user1/s/user1/u1/' /etc/passwd

awk 一般用于对文本内容进行统计、按需要的格式进行输出
	cut  命令:cut -d:-f 1 /etc/passwd
	awk 命令:awk -F: ’/wd$/{print $1}'/etc/passwd


sed的工作方式是:
	将文件以行为单位读取到内存(模式空间)
	使用sed的每个脚本对该行进行操作
	处理完成后输出该行
	sed 默认只执行一次

sed 的替换命令s:
	sed 's/old/new/' filename
	sed -e 's/old/new/' -e  's/old/new/' filename ...
	sed -i 's/old/new/'  's/old/new/' filename

带正则表达式的替换命令
	grep root  /etc/passwd  |sed  ’s/^root//’  将匹配pass文件中以root开头的词组替换成空
	sed ’s /正则表达式/new/‘filename
	sed -r ’s/扩展正则表达式/new/‘filename

sed  's/ab*/!/b' bfile

sed  -r 's/ab*/!/b' bfile   这个是bfile 文件中的ab 出现0次或者1次     替换成!

sed -r 's/ab+/!/' bfile     这个是将bfile文件中的ab出现1次或者多次  替换!

sed -r 's/ab?/!/' bfile      这个是将bfile文件中的ab出现0次或者1次    替换成!

sed -r 's/a|b/!/' bfile      这个是将bfile文件中的a或者b  替换成!

sed -r 's/(a.*e)/\1: \1/' bfile  这个是将cfile文中的abcde 在复制一份,然后中间在加一个冒号
[root@harbor ~]# sed -r 's/(a.*e)/\1:\1/' bfile
abcde:abcde
[root@harbor ~]# 

sed的替换命令加强版
全局替换
head  -5 /etc/passwd | sed 's/root/!!!!/g'     对passwd出现的一行,进行全部匹配,然后将root替换成!!!!
head  -5 /etc/passwd | sed 's/root/!!!!/2'     对passwd这一行,只对第二次匹配的参数进行替换成!!!!
head  -5 /etc/passwd | sed -n 's/root/!!!!/p'    对passwd出现的一行,进行全部匹配,然后将root替换成!!!! 然后打印出来
head  -5 /etc/passwd | sed -n 's/root/!!!!/w /tmp/a.txt' 对passwd出现的一行,将root替换成!!!,然后保存到a.txt文件中
标志位
--------------
数字,第几次出现才进行替换
g,每次出现都进行替换
p打印模式空间的内容
sed -n 'script' filename 阻止默认输出
w file 将模式空间
---------------
寻址
---------------
默认对每行进行操作,增加寻址后对匹配的行进行操作
/正则表达式/s/old/new/g
行号s/old/new/g
行号可以是具体的行,也可以是最后一行$ 符号
head  -5 /etc/passwd | sed '1s/root/!!!!/g'       限制第一行,进行匹配替换
head  -5 /etc/passwd | sed '1,3s/root/!!!!/g'    从第一行到第三行,进行匹配替换
head  -5 /etc/passwd | sed '1,$s/root/!!!!/g'    从第一行到最后一行,进行匹配替换 
可以使用两个寻址符号,也可以混合使用行号和正则地址

分组
寻址可以匹配多条命令
/regular/ {s/old/new/;s/old/new/}

sed脚本文件
sed -f sedscript filename


sed的其他命令

删除命令 d
	删除模式空间内容,改变脚本的控制流, 读取新的输入行
	sed '/ab/d'  bfile


追加 a 、插入 i、更改 c
	sed '/a/i hello'   bfile   在匹配a的下一行插入,hello
	sed '/a/c hello'  bfile   在匹配a的的同时修改成hello
	sed '/a/a hello ' bfile   在匹配a的同时增加hello这一行
		

打印 p
	sed '/a/p' bfile   只要匹配了a就会在输出一次
下一行 n
	用于奇偶行数

读文件 r 写文件w
	sed '/a/r afile ' bfile  >/tmp/cfile
	sed '/a/w ' bfile
退出命令 q

哪个效率比较高
	sed 1 10000 > lines.txt
	time sed -n '1,10p'  lines.txt
	time sed -n '10q'  lines.txt

4、Sed 多行处理模式

多行模式处理命令N、D、P

N 将下一行加入到模式空间
[root@harbor test]# sed 'N' a.txt      
hel
lo
由于hello显示成两行,是因hel \n加了换行符,为了将hello替换成其他内容,可以如下操作
[root@harbor test]# sed 'N;s/hel\nlo/!!!/' a.txt
!!!
其中也可以通过.来替换换行符
[root@harbor test]# sed 'N;s/hel.lo/!!!/' a.txt
!!!

D 删除模式空间中的第一个字符到第一个换行符
P 打印模式空间中的第一个字符到第一个换行符
[root@harbor test]# sed 'N;s/\n//;s/hello bash/fei sed\n/;P;D' b.txt
fei sed
 fei sed

5、Sed保持空间

保持空间也是多行的一种操作模式
将内容暂存在保持空间,便于做多行处理

结构方式

文本文件---模式空间----保持空间

h和H将模式空间内容存放到保持空间
小h覆盖模式,H追加模式
g和G将保持空间内容取出到模式空间
g覆盖模式,G追加模式
x交换模式空间和保持空间内容

[root@harbor ~]# head -6  /etc/passwd | cat -n  | tac
     6	sync:x:5:0:sync:/sbin:/bin/sync
     5	lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     4	adm:x:3:4:adm:/var/adm:/sbin/nologin
     3	daemon:x:2:2:daemon:/sbin:/sbin/nologin
     2	bin:x:1:1:bin:/bin:/sbin/nologin
     1	root:x:0:0:root:/root:/bin/bash


cat -n 显示 行数

tac 倒数

[root@harbor ~]# cat  -n /etc/passwd | head -6 | sed -n '1h;1!G;$!x;$p'
     6	sync:x:5:0:sync:/sbin:/bin/sync
     5	lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     4	adm:x:3:4:adm:/var/adm:/sbin/nologin
     3	daemon:x:2:2:daemon:/sbin:/sbin/nologin
     2	bin:x:1:1:bin:/bin:/sbin/nologin
     1	root:x:0:0:root:/root:/bin/bash

[root@harbor ~]# cat  -n /etc/passwd | head -6 | sed  '1!G;h;$!d'
     6	sync:x:5:0:sync:/sbin:/bin/sync
     5	lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     4	adm:x:3:4:adm:/var/adm:/sbin/nologin
     3	daemon:x:2:2:daemon:/sbin:/sbin/nologin
     2	bin:x:1:1:bin:/bin:/sbin/nologin
     1	root:x:0:0:root:/root:/bin/bash
You have mail in /var/spool/mail/root



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值