当前位置:网站首页>How to delete a specific line from a text file using the SED command?
How to delete a specific line from a text file using the SED command?
2022-07-03 16:46:00 【wljslmz】
You can use Vim or Nano Wait for the text editor , Enter the file and delete the required lines , however , This method is not suitable for use bash Scripts are automated .
contrary , You can use powerful sed Command line editor and delete lines that meet specific conditions , Of course , You can go to shell Use in script sed.
Delete the... From the file n That's ok
Imagine , You must delete the... In the file 7 Line number , You can use... Like this sed command :
sed -i '7d' filename
Let me explain how it works :
- -i: This option enables in place editing , By default ,sed Only output will be displayed . Use this -i Options , It will modify the actual file without displaying it on the display .
- 7d: here 7 Is the line number ,d Indicates to delete the line .
- filename: This is the file you want to modify , If the file is in another directory , You can also provide absolute or relative paths .
Let me use a real example to show , This one is called wljslmz.txt Sample text file :
Learning technology
Go to the network technology alliance station
Find tutorial
Also on the network technology alliance station
Increase knowledge
Also on the network technology alliance station
Add salary
You must go to the network technology alliance station
To delete the 4 That's ok , I use :
sed '4d' agatha.txt
This will cause the following display to be deleted “ Also on the network technology alliance station ” That's ok :
Learning technology
Go to the network technology alliance station
Find tutorial
Increase knowledge
Also on the network technology alliance station
Add salary
You must go to the network technology alliance station
Now? , Suppose you want to be in Linux Delete the first line of the file on the command line , Use... Like this sed:
sed -i '1d' filename
If you want to view the operation results without modifying the file itself , Please do not use -ised Command options .
Use sed Delete last line
You learned to delete specific lines , But what if you need to delete the last line ?
You may always use wc Command to get the total number of rows in the file , And compare this number with sed Use it together . however ,sed There is a special method to delete the last line of the file , You don't have to worry about the number of files anymore .
sed -i '$d' filename.txt
Delete a series of lines
Similar to what you saw before , You can also delete a series of lines from the file .
Suppose you have to delete from 11 To 15 The line of , You can provide such a range :
sed -i '11,15d' filename
Please note that , It will also delete section 11 Xing He 15 That's ok , Not just the rows between them .
Delete lines that contain strings
You can also use it sed Delete all lines that contain a given string or match a specific pattern .
The following command will delete all containing words ‘string’ The line of :
sed -i '/string/d' filename
Please note that , This is a case sensitive search , You can also use regular expression patterns instead of regular strings .
Delete lines beginning with words
If you want to delete all lines that begin with a specific word or letter , Just provide the following regular expression pattern :
sed -i '/^word/d' filename
Additional tips : Delete all blank lines
Before I finish this article , Let me quickly share how to use sed Delete all blank lines :
sed -i '/^$/d' filename
边栏推荐
- Add color to the interface automation test framework and realize the enterprise wechat test report
- Hands on in-depth learning notes (XIV) 3.7 Simple implementation of softmax regression
- What material is 13crmo4-5 equivalent to in China? 13crmo4-5 chemical composition 13crmo4-5 mechanical properties
- Hong Kong Polytechnic University | data efficient reinforcement learning and adaptive optimal perimeter control of network traffic dynamics
- 8 cool visual charts to quickly write the visual analysis report that the boss likes to see
- Top k questions of interview
- Mysql 单表字段重复数据取最新一条sql语句
- What material is sa537cl2 equivalent to in China? Sa537cl2 corresponding material
- CC2530 common registers for timer 1
- Everyone in remote office works together to realize cooperative editing of materials and development of documents | community essay solicitation
猜你喜欢
![[combinatorics] polynomial theorem (polynomial theorem | polynomial theorem proof | polynomial theorem inference 1 item number is the number of non negative integer solutions | polynomial theorem infe](/img/9d/6118b699c0d90810638f9b08d4f80a.jpg)
[combinatorics] polynomial theorem (polynomial theorem | polynomial theorem proof | polynomial theorem inference 1 item number is the number of non negative integer solutions | polynomial theorem infe

Add color to the interface automation test framework and realize the enterprise wechat test report

What material is 12cr1movr? Chemical property analysis of pressure vessel steel plate 12cr1movr

NSQ source code installation and operation process

What kind of material is 14Cr1MoR? Analysis of chemical composition and mechanical properties of 14Cr1MoR

什么是质押池,如何进行质押呢?

NLP四范式:范式一:非神经网络时代的完全监督学习(特征工程);范式二:基于神经网络的完全监督学习(架构工程);范式三:预训练,精调范式(目标工程);范式四:预训练,提示,预测范式(Prompt工程)

CC2530 common registers for timer 1

2022.02.14_ Daily question leetcode five hundred and forty

Deep understanding of grouping sets statements in SQL
随机推荐
Aike AI frontier promotion (7.3)
NLP四范式:范式一:非神经网络时代的完全监督学习(特征工程);范式二:基于神经网络的完全监督学习(架构工程);范式三:预训练,精调范式(目标工程);范式四:预训练,提示,预测范式(Prompt工程)
Recommendation of good books on learning QT programming
Pointcut expression
2022爱分析· 国央企数字化厂商全景报告
Central South University | through exploration and understanding: find interpretable features with deep reinforcement learning
IDEA-配置插件
跟我学企业级flutter项目:简化框架demo参考
Develop team OKR in the way of "crowdfunding"
一台服务器最大并发 tcp 连接数多少?65535?
Mysql 将逗号隔开的属性字段数据由列转行
2022.02.14_ Daily question leetcode five hundred and forty
mysql用户管理
What is the pledge pool and how to pledge?
ucore概述
Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
Hands on in-depth learning notes (XIV) 3.7 Simple implementation of softmax regression
8个酷炫可视化图表,快速写出老板爱看的可视化分析报告
What is the material of sa302grc? American standard container plate sa302grc chemical composition
Alibaba P8 painstakingly sorted it out. Summary of APP UI automated testing ideas. Check it out