当前位置:网站首页>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__
边栏推荐
- C杂讲 浅拷贝 与 深拷贝
- 15 medical registration system_ [appointment registration]
- Canoe cannot automatically identify serial port number? Then encapsulate a DLL so that it must work
- Constants and pointers
- A necessary soft skill for Software Test Engineers: structured thinking
- MySQL real battle optimization expert 08 production experience: how to observe the machine performance 360 degrees without dead angle in the process of database pressure test?
- 112 pages of mathematical knowledge sorting! Machine learning - a review of fundamentals of mathematics pptx
- [one click] it only takes 30s to build a blog with one click - QT graphical tool
- 软件测试工程师必备之软技能:结构化思维
- Download address of canoe, download and activation of can demo 16, and appendix of all canoe software versions
猜你喜欢
Carolyn Rosé博士的社交互通演讲记录
[NLP] bert4vec: a sentence vector generation tool based on pre training
Download address of canoe, download and activation of can demo 16, and appendix of all canoe software versions
jar运行报错no main manifest attribute
在CANoe中通過Panel面板控制Test Module 運行(初級)
嵌入式开发中的防御性C语言编程
What should the redis cluster solution do? What are the plans?
docker MySQL解决时区问题
[one click] it only takes 30s to build a blog with one click - QT graphical tool
Some thoughts on the study of 51 single chip microcomputer
随机推荐
Download address of canoe, download and activation of can demo 16, and appendix of all canoe software versions
Release of the sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
MySQL combat optimization expert 03 uses a data update process to preliminarily understand the architecture design of InnoDB storage engine
How can I take a shortcut to learn C language in college
MySQL实战优化高手11 从数据的增删改开始讲起,回顾一下Buffer Pool在数据库里的地位
MySQL實戰優化高手04 借著更新語句在InnoDB存儲引擎中的執行流程,聊聊binlog是什麼?
cmooc互联网+教育
CAPL script printing functions write, writeex, writelineex, writetolog, writetologex, writedbglevel do you really know which one to use under what circumstances?
CANoe CAPL文件操作目录合集
If someone asks you about the consistency of database cache, send this article directly to him
PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
Compress decompress
AI的路线和资源
CAPL script pair High level operation of INI configuration file
MySQL实战优化高手02 为了执行SQL语句,你知道MySQL用了什么样的架构设计吗?
Which is the better prospect for mechanical engineer or Electrical Engineer?
Target detection -- yolov2 paper intensive reading
South China Technology stack cnn+bilstm+attention
再有人问你数据库缓存一致性的问题,直接把这篇文章发给他
MySQL实战优化高手05 生产经验:真实生产环境下的数据库机器配置如何规划?