目录

  • 前言
  • 一.sed功能简介
  • 二.sed命令格式和参数
  • 2.1 扩展 Perl 命令格式和参数
  • 三.实例练习
  • 3.1 sed命令 --- 动作
  • (1)【a】append --- 第一行之后添加一行
  • (2)【a】append --- 第一行之前添加一行
  • (3)【a】append --- 使用UNIX换行符\n插入多行
  • (4)【d】delete --- 删除第2,3行
  • (5)【e】edit --- 连续编辑 删除【d】后替换【s】
  • (6)【p】print --- 打印第2,3行
  • (7)【s】替换 --- 把etl换成lte
  • (8)【s】替换 --- 匹配数据
  • (9)【s】替换 --- \n替换为空格
  • (10)【s】替换 --- ,替换为\n
  • 四. 应用场景
  • 参考文章



相关链接


前言

文章内容主要来自 1.Linux Sed命令详解+如何替换换行符"\n"(很多面试问道) — _黄文臣 新增了部分内容,优化了排版格式,并且经过练习整理到自己笔记中,方便查阅


一.sed功能简介

  • sed是一项Linux指令,功能同awk类似,差别在于,sed简单,对列处理的功能要差一些,awk的功能复杂,对列处理的功能比较强大
  • sed是一个强大的文本处理工具,可以采用正则匹配,对文本进行插入删除修改等操作
  • sed处理的时候,一次处理一行,每一次把当前处理的存放在临时缓冲区,处理完后输出缓冲区内容到屏幕,然后把下一行读入缓冲区,如此重复,直到结尾。

二.sed命令格式和参数

#带引号 "引号内全部当做文本处理"
sed [-n/e/f/r/i] [动作] 文件					#sed命令_格式
sed -i "s/where /--where /i" ACS_USER.sql 	#sed命令_实例
#不带引号 部分字符需要加\进行转义,如【\】【 】
sed [-n/e/f/r/i] "[动作]" 文件				#sed命令_格式
sed -i s/where\ /where\ /g ACS_USER.sql		#sed命令_实例

# sed命令_参数说明
-n 安静模式,在sed处理的时候,所有来自STDIN的数据都会被输出到终端,加上-n会只输出处理的哪行
-e 直接在命令列上进行sed动作编辑
-f 直接将sed的动作写在文件内
-r sed动作支持延伸的正则表达(默认只是基础正则)
-i 直接修改文件内容(慎用,尤其是用系统文件做练习的时候)

# sed命令_动作说明
a = append 		= 增加,在当前行的下一行增加
c = ? 			= 取代,取代n1到n2之间的行
d = delete 		= 删除
e = edit		= 连续编辑
i = insert 		= 插入,目前行的上一行插入
p = print 		= 打印,常常与-n使用
s = substitute 	= 取代
	#替换时大小写匹配问题
	/g 结尾 : s/old/new/g :匹配时,对old大小写敏感
	/i 结尾 :s/old/new/i : 匹配时,对old大小写不敏感
  • 建议
  • sed -i s/old/new/g file 替换文件前先执行 sed s/old/new/g file 检查一下替换结果,避免造成意外,替换其他内容

2.1 扩展 Perl 命令格式和参数

#带引号 "引号内全部当做文本处理"
perl [-p/i/e] [动作] 文件									#perl命令_格式
perl -i "s/where /--where /i" ACS_USER.sql ACS_TRADE.sql 	#perl命令_实例

#不带引号 部分字符需要加\进行转义,如【\】【 】
perl [-n/e/f/r/i] "[动作]" 文件								#perl命令_格式
perl -i s/where\ /where\ /g ACS_USER.sql ACS_TRADE.sql		#perl命令_格式

# perl命令_参数说明
-p 表示匹配文件的每一行
-i 表示把替换的结果写回到原文件
-e 表示后面的程序都写在一行上

# perl命令_动作说明
s = substitute 	= 取代
	#替换时大小写匹配问题
	/g 结尾 : s/old/new/g :匹配时,对old大小写敏感
	/i 结尾 :s/old/new/i : 匹配时,对old大小写不敏感

三.实例练习

3.1 sed命令 — 动作

# sed命令_动作说明
a = append 		= 增加,在当前行的下一行增加
c = ? 			= 取代,取代n1到n2之间的行
d = delete 		= 删除
e = edit		= 连续编辑
i = insert 		= 插入,目前行的上一行插入
p = print 		= 打印,常常与-n使用
s = substitute 	= 取代
	#替换时大小写匹配问题
	/g 结尾 : s/old/new/g :匹配时,对old大小写敏感
	/i 结尾 :s/old/new/i : 匹配时,对old大小写不敏感
  • 准备练习文件
#切换到练习目录
cd /home/tmp/practice
#将最近5条用户或终端的登录情况 写入file.txt文件中(覆盖)
last -n 5 > file.txt


[etl@localhost practice]$ cat file.txt 
etl      pts/21       50.1.49.71       Fri Jul 24 17:10   still logged in   
root	 pts/22       50.1.49.72       Fri Jul 24 17:04   still logged in   
etl      pts/23       50.1.49.73       Fri Jul 24 17:03 - 17:03  (00:00)    
etl      pts/24       50.1.49.74       Fri Jul 24 16:52   still logged in   
etl      pts/25       50.1.49.75       Fri Jul 24 16:45   still logged in   

wtmp begins Tue Jun  9 07:05:16 2020

参考2.Linux 命令之last命令详解


(1)【a】append — 第一行之后添加一行

[etl@localhost practice]$ cat file.txt 
etl      pts/21       50.1.49.71       Fri Jul 24 17:10   still logged in   
root	 pts/22       50.1.49.72       Fri Jul 24 17:04   still logged in   
etl      pts/23       50.1.49.73       Fri Jul 24 17:03 - 17:03  (00:00)    
etl      pts/24       50.1.49.74       Fri Jul 24 16:52   still logged in   
etl      pts/25       50.1.49.75       Fri Jul 24 16:45   still logged in   

wtmp begins Tue Jun  9 07:05:16 2020


[etl@localhost practice]$ sed "1a add text" file.txt
etl      pts/21       50.1.49.71       Fri Jul 24 17:10   still logged in   
add text
root	 pts/22       50.1.49.72       Fri Jul 24 17:04   still logged in   
etl      pts/23       50.1.49.73       Fri Jul 24 17:03 - 17:03  (00:00)    
etl      pts/24       50.1.49.74       Fri Jul 24 16:52   still logged in   
etl      pts/25       50.1.49.75       Fri Jul 24 16:45   still logged in   

wtmp begins Tue Jun  9 07:05:16 2020

(2)【a】append — 第一行之前添加一行

[etl@localhost practice]$ cat file.txt 
etl      pts/21       50.1.49.71       Fri Jul 24 17:10   still logged in   
root	 pts/22       50.1.49.72       Fri Jul 24 17:04   still logged in   
etl      pts/23       50.1.49.73       Fri Jul 24 17:03 - 17:03  (00:00)    
etl      pts/24       50.1.49.74       Fri Jul 24 16:52   still logged in   
etl      pts/25       50.1.49.75       Fri Jul 24 16:45   still logged in   

wtmp begins Tue Jun  9 07:05:16 2020


[etl@localhost practice]$ sed "1i add text" file.txt 
add text
etl      pts/21       50.1.49.71       Fri Jul 24 17:10   still logged in   
root	 pts/22       50.1.49.72       Fri Jul 24 17:04   still logged in   
etl      pts/23       50.1.49.73       Fri Jul 24 17:03 - 17:03  (00:00)    
etl      pts/24       50.1.49.74       Fri Jul 24 16:52   still logged in   
etl      pts/25       50.1.49.75       Fri Jul 24 16:45   still logged in   

wtmp begins Tue Jun  9 07:05:16 2020

(3)【a】append — 使用UNIX换行符\n插入多行

[etl@localhost practice]$ cat file.txt 
etl      pts/21       50.1.49.71       Fri Jul 24 17:10   still logged in   
root	 pts/22       50.1.49.72       Fri Jul 24 17:04   still logged in   
etl      pts/23       50.1.49.73       Fri Jul 24 17:03 - 17:03  (00:00)    
etl      pts/24       50.1.49.74       Fri Jul 24 16:52   still logged in   
etl      pts/25       50.1.49.75       Fri Jul 24 16:45   still logged in   

wtmp begins Tue Jun  9 07:05:16 2020


[etl@localhost practice]$ sed "2afirst\nsecond" file.txt 
etl      pts/21       50.1.49.71       Fri Jul 24 17:10   still logged in   
root	 pts/22       50.1.49.72       Fri Jul 24 17:04   still logged in   
first
second
etl      pts/23       50.1.49.73       Fri Jul 24 17:03 - 17:03  (00:00)    
etl      pts/24       50.1.49.74       Fri Jul 24 16:52   still logged in   
etl      pts/25       50.1.49.75       Fri Jul 24 16:45   still logged in   

wtmp begins Tue Jun  9 07:05:16 2020

(4)【d】delete — 删除第2,3行

[etl@localhost practice]$ cat file.txt 
etl      pts/21       50.1.49.71       Fri Jul 24 17:10   still logged in   
root	 pts/22       50.1.49.72       Fri Jul 24 17:04   still logged in   
etl      pts/23       50.1.49.73       Fri Jul 24 17:03 - 17:03  (00:00)    
etl      pts/24       50.1.49.74       Fri Jul 24 16:52   still logged in   
etl      pts/25       50.1.49.75       Fri Jul 24 16:45   still logged in   

wtmp begins Tue Jun  9 07:05:16 2020


[etl@localhost practice]$ sed "2,3d" file.txt 
etl      pts/21       50.1.49.71       Fri Jul 24 17:10   still logged in   
etl      pts/24       50.1.49.74       Fri Jul 24 16:52   still logged in   
etl      pts/25       50.1.49.75       Fri Jul 24 16:45   still logged in   

wtmp begins Tue Jun  9 07:05:16 2020

(5)【e】edit — 连续编辑 删除【d】后替换【s】

[etl@localhost practice]$ cat file.txt 
etl      pts/21       50.1.49.71       Fri Jul 24 17:10   still logged in   
root	 pts/22       50.1.49.72       Fri Jul 24 17:04   still logged in   
etl      pts/23       50.1.49.73       Fri Jul 24 17:03 - 17:03  (00:00)    
etl      pts/24       50.1.49.74       Fri Jul 24 16:52   still logged in   
etl      pts/25       50.1.49.75       Fri Jul 24 16:45   still logged in   

wtmp begins Tue Jun  9 07:05:16 2020


[etl@localhost practice]$ sed -e "2d" -e "s/ETL/lte/i" file.txt 
lte      pts/21       50.1.49.71       Fri Jul 24 17:10   still logged in   
lte      pts/23       50.1.49.73       Fri Jul 24 17:03 - 17:03  (00:00)    
lte      pts/24       50.1.49.74       Fri Jul 24 16:52   still logged in   
lte      pts/25       50.1.49.75       Fri Jul 24 16:45   still logged in

(6)【p】print — 打印第2,3行

[etl@localhost practice]$ cat file.txt 
etl      pts/21       50.1.49.71       Fri Jul 24 17:10   still logged in   
root	 pts/22       50.1.49.72       Fri Jul 24 17:04   still logged in   
etl      pts/23       50.1.49.73       Fri Jul 24 17:03 - 17:03  (00:00)    
etl      pts/24       50.1.49.74       Fri Jul 24 16:52   still logged in   
etl      pts/25       50.1.49.75       Fri Jul 24 16:45   still logged in   

wtmp begins Tue Jun  9 07:05:16 2020


[etl@localhost practice]$ sed -n "2,3p" file.txt 
root	 pts/22       50.1.49.72       Fri Jul 24 17:04   still logged in   
etl      pts/23       50.1.49.73       Fri Jul 24 17:03 - 17:03  (00:00)

(7)【s】替换 — 把etl换成lte

[etl@localhost practice]$ cat file.txt 
etl      pts/21       50.1.49.71       Fri Jul 24 17:10   still logged in   
root	 pts/22       50.1.49.72       Fri Jul 24 17:04   still logged in   
etl      pts/23       50.1.49.73       Fri Jul 24 17:03 - 17:03  (00:00)    
etl      pts/24       50.1.49.74       Fri Jul 24 16:52   still logged in   
etl      pts/25       50.1.49.75       Fri Jul 24 16:45   still logged in   

wtmp begins Tue Jun  9 07:05:16 2020

(8)【s】替换 — 匹配数据

[etl@localhost practice]$ cat file.txt 
etl      pts/21       50.1.49.71       Fri Jul 24 17:10   still logged in   
root	 pts/22       50.1.49.72       Fri Jul 24 17:04   still logged in   
etl      pts/23       50.1.49.73       Fri Jul 24 17:03 - 17:03  (00:00)    
etl      pts/24       50.1.49.74       Fri Jul 24 16:52   still logged in   
etl      pts/25       50.1.49.75       Fri Jul 24 16:45   still logged in   

wtmp begins Tue Jun  9 07:05:16 2020

#/i 大小写不敏感
[etl@localhost practice]$ sed -e "s/ETL/lte/i" file.txt
lte      pts/21       50.1.49.71       Fri Jul 24 17:10   still logged in   
root	 pts/22       50.1.49.72       Fri Jul 24 17:04   still logged in   
lte      pts/23       50.1.49.73       Fri Jul 24 17:03 - 17:03  (00:00)    
lte      pts/24       50.1.49.74       Fri Jul 24 16:52   still logged in   
lte      pts/25       50.1.49.75       Fri Jul 24 16:45   still logged in   

#/g 大小写敏感
wtmp begins Tue Jun  9 07:05:16 2020
[etl@localhost practice]$ sed -e "s/ETL/lte/g" file.txt
etl      pts/21       50.1.49.71       Fri Jul 24 17:10   still logged in   
root	 pts/22       50.1.49.72       Fri Jul 24 17:04   still logged in   
etl      pts/23       50.1.49.73       Fri Jul 24 17:03 - 17:03  (00:00)    
etl      pts/24       50.1.49.74       Fri Jul 24 16:52   still logged in   
etl      pts/25       50.1.49.75       Fri Jul 24 16:45   still logged in   

wtmp begins Tue Jun  9 07:05:16 2020

[etl@localhost practice]$ sed -e "s/etl/lte/g" file.txt
lte      pts/21       50.1.49.71       Fri Jul 24 17:10   still logged in   
root	 pts/22       50.1.49.72       Fri Jul 24 17:04   still logged in   
lte      pts/23       50.1.49.73       Fri Jul 24 17:03 - 17:03  (00:00)    
lte      pts/24       50.1.49.74       Fri Jul 24 16:52   still logged in   
lte      pts/25       50.1.49.75       Fri Jul 24 16:45   still logged in   

wtmp begins Tue Jun  9 07:05:16 2020

(9)【s】替换 — \n替换为空格

[etl@localhost practice]$ cat ACS_USER.sql 
SELECT
A,
B,
C,
D
FROM SDATA.ACS_USER
WHERE USER_NAME=GROUPIES_M

[etl@localhost practice]$ sed ':a;N;$!ba;s/\n/ /g' ACS_USER.sql 
SELECT A, B, C, D FROM SDATA.ACS_USER WHERE USER_NAME=GROUPIES_M

(10)【s】替换 — ,替换为\n

[etl@localhost practice]$ cat ACS_USER.sql 
SELECT
A,
B,
C,
D
FROM SDATA.ACS_USER
WHERE USER_NAME=GROUPIES_M

[etl@localhost practice]$ sed ':a;N;$!ba;s/,/\n/g' ACS_USER.sql 
SELECT
A

B

C

D
FROM SDATA.ACS_USER
WHERE USER_NAME=GROUPIES_M

四. 应用场景

一般应用于S层数据第一次时抽取数据全量抽取后,后续任务调度按照增量抽取
使用以下命令批量将抽数脚本中的WHERE条件打开

sed -e "s/--//i" xxx.sql

参考文章

1.Linux Sed命令详解+如何替换换行符"\n"(很多面试问道) — _黄文臣 2.Linux命令之last


20/07/24

M