当前位置:网站首页>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
边栏推荐
- Web crawler knowledge day03
- Aike AI frontier promotion (7.3)
- What material is 13crmo4-5 equivalent to in China? 13crmo4-5 chemical composition 13crmo4-5 mechanical properties
- 手机注册股票开户安全吗 开户需要钱吗
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
- 斑马识别成狗,AI犯错的原因被斯坦福找到了
- CC2530 common registers for ADC single channel conversion
- Leetcode binary search tree
- 网络安全web渗透技术
- The word backspace key cannot delete the selected text, so you can only press Delete
猜你喜欢
Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
2022.02.14_ Daily question leetcode five hundred and forty
arduino-esp32:LVGL项目(一)整体框架
PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
Cocos Creator 2. X automatic packaging (build + compile)
Explore Cassandra's decentralized distributed architecture
智慧之道(知行合一)
Data driving of appium framework for mobile terminal automated testing
IDEA-配置插件
Mysql 单表字段重复数据取最新一条sql语句
随机推荐
[Jianzhi offer] 58 - ii Rotate string left
How to set up SVN server on this machine
[solved] access denied for user 'root' @ 'localhost' (using password: yes)
跟我学企业级flutter项目:简化框架demo参考
远程办公之如何推进跨部门项目协作 | 社区征文
于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
function overloading
PHP converts a one-dimensional array into a two-dimensional array
利用MySQL中的乐观锁和悲观锁实现分布式锁
特征多项式与常系数齐次线性递推
Unity project optimization case 1
To resist 7-Zip, list "three sins"? Netizen: "is the third key?"
Threejs Part 2: vertex concept, geometry structure
斑馬識別成狗,AI犯錯的原因被斯坦福找到了
Develop team OKR in the way of "crowdfunding"
Golang anonymous function use
[combinatorics] non descending path problem (number of non descending paths with constraints)
Alibaba P8 painstakingly sorted it out. Summary of APP UI automated testing ideas. Check it out
浅谈拉格朗日插值及其应用
香港理工大学|数据高效的强化学习和网络流量动态的自适应最优周界控制