当前位置:网站首页>Shell script learning notes -- shell operation on files sed
Shell script learning notes -- shell operation on files sed
2022-04-23 02:47:00 【XL's Princess】
One 、sed Command Introduction
sed yes liunx An external command provided in , It's a line ( flow ) Editor , Non interactive operation of adding, deleting, modifying, inserting and querying the contents of the file , Users can only enter editing commands on the command line , Specify a filename , Then look at the output on the screen , It is essentially different from text editor .
The difference is that :
Text editor : Editing objects are files
Line editor : The edit object is a line in the file
Two 、 grammar
sed [-hnV][-e<script>][-f<script file >][ text file ]
Parameter description :
-e<script> or --expression=<script> As specified in the options script To process the input text file .
-f<script file > or --file=<script file > As specified in the options script File to process the input text file .
-i Edit file contents
-i.bak Create while modifying .bak Backup file
-r Use extended regular expressions
! Take the opposite ( Follow the pattern condition and shell Somewhat different )
-h or --help Display help .
-n or --quiet or --silent Show only script The result of the treatment .
-V or --version Display version information . Action description :
a : newly added , a You can use a string after , And these strings will appear on a new line ( The next line at the moment )~
c : replace , c You can use a string after , These strings can replace n1,n2 Between the lines !
d : Delete , Because it's deletion , therefore d There's usually nothing in the back ;
i : Insert , i You can use a string after , And these strings will appear on a new line ( The current line );
p : Print , Will also print out some selected data . Usually p And parameters sed -n Run together ~
s : replace , Can be replaced directly by work ! Usually the s The action can be combined with normal expression ! for example 1,20s/old/new/g That's it !
3、 ... and 、 Example demonstration
1) Additional :sed ‘a\ Additional content ’ file name ‘\’ It can be omitted
# Add a line after each line “hello world”
[root@localhost shell]# sed 'a\hello world' sed.sh
sdf
hello world
sgajglasjg
hello world
wewgjasldjg
hello world
sdnval;jgqwe
hello world
14213rwfsq92j
hello world
hello world
# stay 2 To 4 Add a line after the line “hello world”
[root@localhost shell]# sed '2,4a\hello world' sed.sh
sdf
sgajglasjg
hello world
wewgjasldjg
hello world
sdnval;jgqwe
hello world
14213rwfsq92j
# stay 4 Add a line after the line “hello world”
[root@localhost shell]# sed '4a\hello world' sed.sh
sdf
sgajglasjg
wewgjasldjg
sdnval;jgqwe
hello world
14213rwfsq92j
# matching “wew” Where the line is , And add a line after it “hello world”
[root@localhost shell]# sed '/wew/a\hello world' sed.sh
sdf
sgajglasjg
wewgjasldjg
hello world
sdnval;jgqwe
14213rwfsq92j
2) Insert :sed ‘i\ Insert content ’ file name
# Insert a line before each line “hello world”
[root@localhost shell]# sed 'i\hello world' sed.sh
hello world
sdf
hello world
sgajglasjg
hello world
wewgjasldjg
hello world
sdnval;jgqwe
hello world
14213rwfsq92j
hello world
# In the 3 Insert a line before the line “hello world”
[root@localhost shell]# sed '3i\hello world' sed.sh
sdf
sgajglasjg
hello world
wewgjasldjg
sdnval;jgqwe
14213rwfsq92j
# stay 2 To 4 Insert a line before the line “hello world”
[root@localhost shell]# sed '2,4i\hello world' sed.sh
sdf
hello world
sgajglasjg
hello world
wewgjasldjg
hello world
sdnval;jgqwe
14213rwfsq92j
# matching “wew” Where the line is , And insert a line before it “hello world”
[root@localhost shell]# sed '/wew/i\hello world' sed.sh
sdf
sgajglasjg
hello world
wewgjasldjg
sdnval;jgqwe
14213rwfsq92j
3) Delete :sed ‘d/ Deleted content ’ file name
# Delete all
[root@localhost shell]# sed 'd' sed.sh
# Delete the third line
[root@localhost shell]# sed '3d' sed.sh
sdf
sgajglasjg
sdnval;jgqwe
14213rwfsq92j
# Delete 3 To 4 That's ok
[root@localhost shell]# sed '3,4d' sed.sh
sdf
sgajglasjg
14213rwfsq92j
# matching “wew” Where the line is
[root@localhost shell]# sed '/wew/d' sed.sh
sdf
sgajglasjg
sdnval;jgqwe
14213rwfsq92j
# Delete nginx.conf In file # The first line may contain # Line or blank line
#‘-r’ Indicates support for regular expressions
#‘^$’ Indicates an empty line
[root@localhost shell]# sed -r '/(^#|#|^$)/d' nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
4) Replace :sed ‘s/ Replaced characters / Content after replacement /’ file name
# Put the first character of each line ‘g’ Replace with ‘A’
[root@localhost shell]# sed 's/g/A/' sed.sh
sdf A
sAajglasjg
wewAjasldjg
sdnval;jAqweg
14213rwfsq92jA
# Put the first character of the third line ‘g’ Replace with ‘A’
[root@localhost shell]# sed '3s/g/A/' sed.sh
sdf g
sgajglasjg
wewAjasldjg
sdnval;jgqweg
14213rwfsq92jg
# take 2 To 4 The first character of the line ‘g’ Replace with ‘A’
[root@localhost shell]# sed '2,4s/g/A/' sed.sh
sdf g
sAajglasjg
wewAjasldjg
sdnval;jAqweg
14213rwfsq92jg
# matching ‘wew’ The first character of the line ‘g’ Replace with ‘A’
[root@localhost shell]# sed '/wew/s/g/A/' sed.sh
sdf g
sgajglasjg
wewAjasldjg
sdnval;jgqweg
14213rwfsq92jg
5) change :sed ‘c\ What's changed ’ file name
# Change all rows to hell oword
[root@localhost shell]# sed 'c\hell oword' sed.sh
hell oword
hell oword
hell oword
hell oword
hell oword
hell oword
# Change the third line to hell oword
[root@localhost shell]# sed '3c\hell oword' sed.sh
sdf g
sgajglasjg
hell oword
sdnval;jgqweg
14213rwfsq92jg
# Change the third to fourth lines to hell oword
[root@localhost shell]# sed '3,4c\hell oword' sed.sh
sdf g
sgajglasjg
hell oword
14213rwfsq92jg
# matching ‘wew’ The line is changed to hell oword
[root@localhost shell]# sed '/wew/c\hell oword' sed.sh
sdf g
sgajglasjg
hell oword
sdnval;jgqweg
14213rwfsq92jg
5) transformation :sed ‘y/ What is being converted / Content of conversion /’ file name
# take a convert to U; take b convert to B; take c convert to L
[root@localhost shell]# sed 'y/abc/UBL/' sed.sh
sdf g
sgUjglUsjg
wewgjUsldjg
sdnvUl;jgqweg
14213rwfsq92jg
6) Print :sed ‘p/ Printed lines ’ file name
7) Edit the structure of the save process :sed -i ’ ’
sed -i.bak Back up the source file
[root@localhost shell]# sed -i '/wew/c\hell oword' sed.sh
[root@localhost shell]# cat sed.sh
sdf g
sgajglasjg
hell oword
sdnval;jgqweg
14213rwfsq92jg
[root@localhost shell]# sed -i.bak '4,5c\hell oword' sed.sh
[root@localhost shell]# ls
ip.sh nginx.conf nginx.sh sed.sh sed.sh.bak while zzbds.sh
[root@localhost shell]# cat sed.sh
sdf g
sgajglasjg
hell oword
hell oword
[root@localhost shell]# cat sed.sh.bak
sdf g
sgajglasjg
hell oword
sdnval;jgqweg
14213rwfsq92jg
Four 、sed Command parsing
command | explain |
---|---|
Numbers | Represents the new text replacement mode |
g | Represents replacing all instances of existing text with new text |
p | Indicates that the original content is printed |
w filename | Write the replacement result to the file |
demonstration
# Numbers , The default is to replace the first... Of each line , Adding numbers means the first few characters
[root@localhost shell]# sed 's/g/A/2' sed.sh
#g Replace all of each line
[root@localhost shell]# sed 's/g/A/g' sed.sh
#p Print
[root@localhost shell]# sed '3s/g/A/p' sed.sh
#w file name Save the modified content filename In file
[root@localhost shell]# sed '3s/g/A/w filename' sed.sh
Suppress memory output , Print on request :sed -n
[root@localhost shell]# sed -n '3s/g/A/p' sed.sh
Execute multiple instructions :sed -e
[root@localhost shell]# sed -e '3s/g/A/p;3s/b/C/p' sed.sh
Specifies the file where the directive is stored :sed -f
First, store the instructions to be executed in a file , Such as the “3s/g/A/p;3s/b/C/p” There is test.txt In file
[root@localhost shell]# sed -f test.txt sed.sh
版权声明
本文为[XL's Princess]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220741454839.html
边栏推荐
- 能做多大的单片机项目程序开发,就代表了你的敲代码的水平
- 定了,今日起,本号粉丝可免费参与网易数据分析培训营!
- Jz76 delete duplicate nodes in linked list
- LeetCode 1450 - 1453
- php+mysql對下拉框搜索的內容修改
- How can enterprises with major hazard installations ensure the completion of the digital construction task of double prevention mechanism by the end of the year
- Flink stream processing engine system learning (III)
- How to solve the complexity of project document management?
- If you want to learn SQL with a Mac, you should give yourself a good reason to buy a Mac and listen to your opinions
- JVM class loader
猜你喜欢
Linux Redis——Redis 数据库缓存服务
SQL server2019 cannot download the required files, which may indicate that the version of the installer is no longer supported. What should I do
A domestic image segmentation project is heavy and open source!
How to solve the complexity of project document management?
Linux Redis ——Redis HA Sentinel 集群搭建详解 & Redis主从部署
Android 高阶面试必问:全局业务和项目的架构设计与重构
Interpretation of the future development of smart agriculture
[unity3d] rolling barrage effect in live broadcasting room
Download the genuine origin Pro 2022 tutorial and how to activate it
hack the box optimum靶机
随机推荐
[XJTU computer network security and management] Lecture 2 password technology
PIP install shutil reports an error
Learn regular expression options, assertions
学习正则表达式选项、断言
【工欲善其事必先利其器】论文编辑及文献管理(Endnote,Latex,JabRef ,overleaf)资源下载及使用指南
Linux Redis ——Redis HA Sentinel 集群搭建详解 & Redis主从部署
16、 Anomaly detection
Log4j knowledge point record
Win view port occupation command line
First day of rhcsa
JZ35 复杂链表的复制
JVM class loader
Jz76 delete duplicate nodes in linked list
基于多态的职工管理系统源码与一些理解
If MySQL / SQL server judges that the table or temporary table exists, it will be deleted
Fashion MNIST dataset classification training
解决 注册谷歌邮箱 gmail 手机号无法用于验证
Yes, from today on, our fans can participate in Netease data analysis training camp for free!
本地远程访问云服务器的jupyter
How to build an integrated industrial Internet plus hazardous safety production management platform?