当前位置:网站首页>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
边栏推荐
- Intelligent agricultural management model
- Fashion MNIST 数据集分类训练
- Yes, from today on, our fans can participate in Netease data analysis training camp for free!
- Leangoo brain map - shared multi person collaborative mind mapping tool
- 【unity3D】直播间滚动式弹幕效果
- Go语言web中间件的使用
- Global, exclusive and local routing guard
- 5W of knowledge points
- Global, exclusive, local Routing Guard
- Devil cold rice 𞓜 078 devil answers the market in Shanghai and Nanjing; Communication and guidance; Winning the country and killing and screening; The purpose of making money; Change other people's op
猜你喜欢

hack the box optimum靶机

Linux redis - redis ha sentinel cluster construction details & redis master-slave deployment

C语言 171. 最近回文数

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

Windows MySQL 8 zip installation

Fashion MNIST 数据集分类训练
![[XJTU计算机网络安全与管理]第二讲 密码技术](/img/b0/263e8dcbfeb2ce9f504a9c8eb76b07.png)
[XJTU计算机网络安全与管理]第二讲 密码技术

How big the program development of single chip microcomputer project can be, it represents your level of knocking code
![[unity3d] rolling barrage effect in live broadcasting room](/img/61/46a7d6c4bf887fca8f088e7673cf2f.png)
[unity3d] rolling barrage effect in live broadcasting room

JVM class loader
随机推荐
PIP install shutil reports an error
工业互联网+危化安全生产综合管理平台怎样建
OCR识别PDF文件
打靶narak
机器学习(周志华) 第十四章概率图模型
全局、獨享、局部路由守衛
Linux Redis ——Redis HA Sentinel 集群搭建详解 & Redis主从部署
Store consumption SMS notification template
LeetCode 1450 - 1453
[wechat applet] set the bottom menu (tabbar) for the applet
Devil cold rice 𞓜 078 devil answers the market in Shanghai and Nanjing; Communication and guidance; Winning the country and killing and screening; The purpose of making money; Change other people's op
1215_ Hello world used by scons
Suggestion: block reference sorting is in the order of keywords
Push data from onenet cloud platform to database
Fashion MNIST dataset classification training
Classification and regression tree of machine learning
重大危险源企业如何保障年底前完成双预防机制数字化建设任务
Halo open source project learning (I): project launch
JDBC JDBC
Day 3 of learning rhcsa