当前位置:网站首页>Sed text processing
Sed text processing
2022-07-06 10:09:00 【wx5caecf2ed0645】
1. Basic overview
sed It's a stream editor , Non interactive editor , It processes one line at a time . When dealing with , Store the currently processed rows in a temporary buffer , call *
by “ Mode space ”(pattern space) Then use sed Command processing buffer contents , After processing , Send the contents of the buffer to the screen . Pick up
Wait for the next line , This is repeated , Until the end of the file . The contents of the document have not changed , Unless you Use redirection to store output . Sed To use from
Edit one or more files automatically ; Simplify file manipulation ; Write conversion program, etc .
2..sed Basic grammar
The first form :stdout | sed [option] "pattern command"
The second form : sed [option] "pattern command" file
3..sed Common options -i -r
sed Related sample files
[[email protected] opt]# cat file.txt
I love shell
I love SHELL
This is test file
1.sed-n、-e Examples of options
# Cancel default output
[[email protected] opt]# sed -n '/shell/p' file.txt
I love shell
# Edit multiple items
[[email protected] opt]# sed -n -e '/shell/p' -e '/SHELL/p' file.txt
I love shell
I love SHELL
2.sed -f Examples of options
# take pattern Write to file
[[email protected] opt]# cat edit.sed
/shell/p
[[email protected] opt]# sed -n -f edit.sed file.txt
3.sed -r Examples of options
[[email protected] opt]# sed -n '/shell|SHELL/p' file.txt
# Extended regular expression
[[email protected] opt]# sed -rn '/shell|SHELL/p' file.txt
I love shell
I love SHELL
4.sed -i Options
[[email protected] sed]# sed -in '/shell/d' file.txt
3.sed pattern
sed -n '10p' passwd
sed -n '10,20p' passwd
sed -n '1,+5p' passwd
sed -n '/^root/p' passwd
sed -n '/^root/,/^ftp/p' passwd
sed -n '/^\[mysql.*/,/^\[mysqld_safe\]/p' /etc/my.cnf|grep -v "^\[.*"
sed -n '2,/\/bin\/sync/p' passwd
1) Print /etc/passwd pass the civil examinations 20 That's ok
sed -n '20p' /etc/passwd
2) Print /etc/passwd From the first 8 OK, let's start , To the first 15 What's at the end of the line
sed -n '8,15p' /etc/passwd
3) Print /etc/passwd From the first 8 OK, let's start , then +5 What's at the end of the line
sed -n '8,+5p' /etc/passwd
4) Print /etc/passwd Match at the beginning of bin The contents of a string
sed -n '/^bin/p' /etc/passwd
5) Print /etc/passwd The middle begins with root Start of line for , To start with ftp The end of the line
sed -n '/^root/,/^ftp/p' /etc/passwd
6) Print /etc/passwd pass the civil examinations 8 OK, let's start , To contain /sbin/nologin The end of the content line
sed -n '8,/\/sbin\/nologin/p' /etc/passwd
4.sed Additional orders
[[email protected] sed]# sed -i '/^root/i server {\n\tlisten 80;\n\tserver_name oldxu.com;\n\tindex index.html;\n\troot /code;\n}' passwd
server {
listen 80;
server_name oldxu.com;
index index.html;
root /code;
}
Examples of exercises in this chapter :
1) passwd Document No 10 Add “Add Line”
sed -i '10a "Add Line"' passwd
2)passwd Document No 10 Go to the first place 20 That's ok , No line is followed by "Test Line"
sed -i '10,20a "Test Line"' passwd
3)passwd The file matches to /bin/bash Add "Insert Line"
sed -i '/\/bin\/bash/a "Insert Line"' passwd
4)passwd The file matches to bin Beginning line , Append "Add Line Before"
sed -i '/^bin/i "Add Line Before"' passwd
5)passwd Every line of the file is preceded by "Insert Line Before"
sed -i 'i "Insert Line Before"' passwd
6) take /etc/fstab The contents of the document are appended to passwd File first 10 After line
sed -i '10r /etc/fstab' passwd
7) take /etc/inittab The contents of the document are appended to passwd File matching /bin/sync The back of the line
sed -i '/\/bin\/sync/r /etc/inittab' passwd
8) take /etc/hosts The contents of the document are appended to passwd In file 10 The back of the line
sed -i '10r /etc/hosts' passwd
9) take passwd The file matches to /bin/bash The line of is appended to /tmp/sed.txt In file
sed -i '/\/bin\/bash/w /tmp/sed.txt' passwd
10) take passwd Document cluster 10 OK, let's start , To match to nfsnobody All lines at the beginning are appended to /tmp/sed-1.txt
sed -i '10,/^nfsnobody/w /tmp/sed-1.txt' passwd
5.sed The delete command
Examples of exercises in this chapter :
1) Delete passwd No 15 That's ok
sed -i '15d' passwd
2) Delete passwd No 8 Go to the first place 14 All the content of the line
sed -i '8,14d' passwd
3) Delete passwd China and Israel /sbin/nologin The line at the end
sed -i '/\/sbin\/nologin$/d' passwd
4) Delete passwd China and Israel bin Beginning line , To ntp All the contents of the first line
sed -i '/^bin/,/^ntp/d' passwd
5) Delete passwd pass the civil examinations 3 So far as ftp All lines at the beginning
sed '3,/^ftp/d' passwd
6) Typical demand : Delete Nginx All comments and blank lines in the configuration file
sed -ri '/^#|^$| #/d' nginx.conf
6.sed Modify the order s///g <-- Replace c modify
sed '/^SELINUX=/c SELINUX=disabled'
1. modify passwd Document No 1 The first one in the line root by ROOT
sed -i '1s/root/ROOT/' passwd
2. modify passwd Document No. 5 Go to the first place 10 All in row /sbin/nologin by /bin/bash
sed -i '5,10s/\/sbin\/nologin/\/bin\/bash/' passwd
sed -i '5,10s#/sbin/nologin#/bin/bash#' passwd
3. modify passwd The file matches with /sbin/nologin The line of , Match to login
It is capitalized LOGIN
sed -i '/\/sbin\/nologin/s#login#LOGIN#g' passwd
sed -i '/\/sbin\/nologin/s/login/LOGIN#g' passwd
4. modify passwd The file matches from to with root Beginning line , To match to bin Start line , modify bin by BIN
sed -i '/^root/,/^bin/s/bin/BIN/g' passwd
5. modify SELINUX=enforcing It is amended as follows SELINUX=disabled.( have access to c Alternative )
sed -i '/^SELINUX=/c SELINUX=disabled' selinux
6. take nginx.conf Add comments to the configuration file . ^ $
sed -i 's/^/# /' nginx.conf
7. Use sed extract eth0 NIC IP Address
ifconfig eth0 | sed -rn '2s/^.*et //p' | sed -rn 's/ ne.*//p'
ifconfig eth0 |sed -nr '2s/(^.*et) (.*) (net.*)/\2/p'
7.sed Script exercises
Requirements describe : Deal with a similar MySQL Profile's my.cnf file .
1. There are several paragraphs in the input file , a pair [ ] For a paragraph .
2. Configure file parameters for each segment , Count the total number .
[[email protected] opt]# sh example.sh
1: client 2
2: server 12
3: mysqld 12
4: mysqld_safe 7
5: embedded 8
6: mysqld-5.5 10
1. Print the contents of each paragraph
sed -n -e '/^\[.*\]/p' sed.txt | sed 's/\[//' | sed 's/\]//'
2. Count the total number of parameters in each section
sed -n '/^\[server\]/,/^\[.*\]/p' sed.txt | sed -r '/^\[|^#|^$/d'|wc -l
The script is
[[email protected] sed]# cat sed.sh
#!/bin/bash
#Author: rzq
#QQ: 0123456789
#Date: 2019-11-06
#FileName: sed.sh
#URL: https://www.jianshu.com
#Description: The test script
#Copyright (C): 2019 All rights reserved
HS () {
num=$(sed -n '/^\[.*]/p' kl.txt|sed 's/\[//'|sed 's/\]//')
}
GS () {
gs=$(sed -n '/^\['$i']/,/^\[.*]/p' kl.txt|sed -r '/^\[|^#|^$/d'|wc -l)
}
HS
b=0
for i in $num
do
let b++
GS
echo "$b $i $gs"
done
Requirements describe : Deal with one ansible Of invtory Host list .
1. Output host group , a pair [ ] For a host group .
2. Output the total number of hosts under each host group .
[[email protected] opt]# sh example.sh
1: web01: Yes 2 Console host
2: web02: Yes 12 Console host
[[email protected] sed]# cat ansible.sh
#!/bin/bash
#Author: rzq
#QQ: 0123456789
#Date: 2019-11-07
#FileName: ansible.sh
#URL: https://www.jianshu.com
#Description: The test script
#Copyright (C): 2019 All rights reserved
fu () {
FW=$(sed -n '/^\[.*]/p' lp.txt|sed 's/\[//' | sed 's/\]//')
}
ip () {
IP=$(sed -n '/^\['$i'/,/^\[.*]/p' lp.txt|sed -r '/^\[|^$/d'|wc -l)
}
fu
num=0
for i in $FW
do
let num++
ip
echo "$num $i $IP"
done
sed
Two kinds of grammar :
sed options '/pattern1/command' file
1、 Options options:
-i Change the document directly
-r Support for extended regular expressions
-f Write the processing command to the file , And then call
-e You can edit multiple operations
-n Cancel the output of default text
2. Pattern matching
string matching
Regular Expression Matching
From where -> Where to go? '/pattern1/,/pattern2/'
3. command command
increase
a # Add
i # Add
r # Read the contents of a file , Added to the /pattern1/ The back of the line
w # Write the contents of a file to a file . >
Delete
d # Delete the matching content , But it needs cooperation -i Option to manipulate the file .
Change
s/// # Replace , Replace what with what , Need to cooperate with -i Option to manipulate the file
s///g # Replace , Global replacement ( Replace all the contents that meet the conditions in a row )
c # modify , You can use regular expressions to match , And then modify Need to cooperate with -i Option to manipulate the file
check
p # The effect of printout . The default action , You have to add . Usually cooperate -n Use it together
__EOF__
边栏推荐
- 好博客好资料记录链接
- Function description of shell command parser
- MySQL的存储引擎
- Teach you how to write the first MCU program hand in hand
- flask运维脚本(长时间运行)
- 嵌入式开发比单片机要难很多?谈谈单片机和嵌入式开发设计经历
- Routes and resources of AI
- CAPL脚本中关于相对路径/绝对路径操作的几个傻傻分不清的内置函数
- MySQL實戰優化高手08 生產經驗:在數據庫的壓測過程中,如何360度無死角觀察機器性能?
- C miscellaneous lecture continued
猜你喜欢

在CANoe中通过Panel面板控制Test Module 运行(高级)

Preliminary introduction to C miscellaneous lecture document
![15 medical registration system_ [appointment registration]](/img/c1/27c7a5aae82783535e5467583bb176.png)
15 medical registration system_ [appointment registration]

Write your own CPU Chapter 10 - learning notes
![[one click] it only takes 30s to build a blog with one click - QT graphical tool](/img/f0/52e1ea33a5abfce24c4a33d107ea05.jpg)
[one click] it only takes 30s to build a blog with one click - QT graphical tool

C杂讲 浅拷贝 与 深拷贝
![13 medical registration system_ [wechat login]](/img/c9/05ad1fc86e02cf51a37c9331938b0a.jpg)
13 medical registration system_ [wechat login]

MySQL实战优化高手11 从数据的增删改开始讲起,回顾一下Buffer Pool在数据库里的地位

CAPL script pair High level operation of INI configuration file

CANoe下载地址以及CAN Demo 16的下载与激活,并附录所有CANoe软件版本
随机推荐
MySQL实战优化高手06 生产经验:互联网公司的生产环境数据库是如何进行性能测试的?
Which is the better prospect for mechanical engineer or Electrical Engineer?
Several errors encountered when installing opencv
Safety notes
The 32 year old programmer left and was admitted by pinduoduo and foreign enterprises. After drying out his annual salary, he sighed: it's hard to choose
Installation of pagoda and deployment of flask project
在CANoe中通过Panel面板控制Test Module 运行(高级)
C杂讲 浅拷贝 与 深拷贝
美疾控中心:美国李斯特菌疫情暴发与冰激凌产品有关
vscode 常用的指令
14 医疗挂号系统_【阿里云OSS、用户认证与就诊人】
Some thoughts on the study of 51 single chip microcomputer
Inject common SQL statement collation
C miscellaneous lecture continued
Can I learn PLC at the age of 33
Keep these four requirements in mind when learning single chip microcomputer with zero foundation and avoid detours
How to make shell script executable
The programming ranking list came out in February. Is the result as you expected?
MySQL实战优化高手09 生产经验:如何为生产环境中的数据库部署监控系统?
Contrôle de l'exécution du module d'essai par panneau dans Canoe (primaire)