当前位置:网站首页>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
边栏推荐
- The problem of removing spaces from strings
- leetcode 烹飪料理
- Difference between relative path and absolute path (often asked in interview)
- JZ35 复杂链表的复制
- Practical combat of industrial defect detection project (II) -- steel surface defect detection based on deep learning framework yolov5
- 【工欲善其事必先利其器】论文编辑及文献管理(Endnote,Latex,JabRef ,overleaf)资源下载及使用指南
- Fashion MNIST 数据集分类训练
- 重大危险源企业如何保障年底前完成双预防机制数字化建设任务
- [wechat applet] set the bottom menu (tabbar) for the applet
- Rhcsa day 4 operation
猜你喜欢

Android high-level interview must ask: overall business and project architecture design and reconstruction

Store consumption SMS notification template

Actual combat of industrial defect detection project (IV) -- ceramic defect detection based on hrnet

高效音乐格式转换工具Music Converter Pro

魔王冷饭||#078 魔王答上海、南京行情;沟通指导;得国和打杀筛选;赚钱的目的;改变别人看法

Day 3 of learning rhcsa

【unity3D】直播间滚动式弹幕效果

Innovation and management based on Scrum

How to solve the complexity of project document management?

机器学习(周志华) 第十四章概率图模型
随机推荐
[suggestion collection] hematemesis sorting out golang interview dry goods 21 questions - hanging interviewer-1
Domestic lightweight Kanban scrum agile project management tool
Intelligent agricultural management model
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
Download the genuine origin Pro 2022 tutorial and how to activate it
Android high-level interview must ask: overall business and project architecture design and reconstruction
The second day of learning rhcsa
Probabilistic model of machine learning
1、 Sequence model
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
First knowledge of C language ~ branch statements
leetcode 烹飪料理
Leangoo brain map - shared multi person collaborative mind mapping tool
Slave should be able to synchronize with the master in tests/integration/replication-psync.tcl
学习正则表达式选项、断言
leetcode 烹饪料理
Six very 6 computer driver managers: what software is good for driver upgrade? Recommended by the best computer driver management software abroad
Day 3 of learning rhcsa
Web learning record (medium)
Rhcsa day 3 operation