当前位置:网站首页>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
边栏推荐
- A survey of state of the art on visual slam
- CC2530 common registers for timer 1
- 爱可可AI前沿推介(7.3)
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
- What is the pledge pool and how to pledge?
- Arduino esp32: overall framework of lvgl project (I)
- What material is sa537cl2? Analysis of mechanical properties of American standard container plate
- MySQL single table field duplicate data takes the latest SQL statement
- 消息队列消息丢失和消息重复发送的处理策略
- Mysql 单表字段重复数据取最新一条sql语句
猜你喜欢

2022.02.14_ Daily question leetcode five hundred and forty

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

PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug

One article takes you to understand machine learning

Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..

Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer

斑马识别成狗,AI犯错的原因被斯坦福找到了

What is the pledge pool and how to pledge?

ucore概述

CC2530 common registers for crystal oscillator settings
随机推荐
What material is 12cr1movr? Chemical property analysis of pressure vessel steel plate 12cr1movr
Netease UI automation test exploration: airtest+poco
What is the maximum number of concurrent TCP connections for a server? 65535?
Difference between JSON and bson
【剑指 Offer】58 - I. 翻转单词顺序
消息队列消息丢失和消息重复发送的处理策略
A survey of state of the art on visual slam
2022.02.14_ Daily question leetcode five hundred and forty
Cocos Creator 2.x 自动打包(构建 + 编译)
14 topics for performance interviews between superiors and subordinates (4)
What is the material of 13mnnimor? 13mnnimor steel plate for medium and low temperature pressure vessels
Threejs Part 2: vertex concept, geometry structure
关于视觉SLAM的最先进技术的调查-A survey of state-of-the-art on visual SLAM
NSQ source code installation and operation process
Learn from me about the enterprise flutter project: simplified framework demo reference
Nifi from introduction to practice (nanny level tutorial) - flow
Google Earth engine (GEE) - daymet v4: daily surface weather data set (1000m resolution) including data acquisition methods for each day
执行脚本不认\r
Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
什么是质押池,如何进行质押呢?