当前位置:网站首页>Various poses for text modification using sed
Various poses for text modification using sed
2022-06-11 18:14:00 【wx61eaae213a986】
SED Introduce
sed(Stream Editor) Stream editor , yes Unix Common command line programs . yes Bell Laboratory, Lee E.McMahon stay 1973 Year to 1974 The development was completed in two years , Currently, it can be used in most operating systems .
sed The emergence of the game is as grep A successor to , because grep It can only be found and replaced simply , But there may be various needs such as deletion ,McMahon Developed a more versatile tool .sed Well known grammatical rules include the use of / Pattern matching , as well as s/// To replace . Tools that exist at the same time ed Together ,sed The grammar of English influenced the later development of ECMAScript and Perl.GNU sed Added a lot of features , Including famous in-place editing.
grammar
to glance at sed Basic syntax :
sed [-nefri] 'command' [file ...]
sed '{[/]<n>|<string>|<regex>[/]}d' <fileName>
sed '{[/]<adr1>[,<adr2>][/]d' <fileName>
- 1.
- 2.
- 3.
The sed utility reads the specified files, or the standard input if no files are specified, modifying the input as specified by a list of commands. The input is then written to the standard output.
sed Get content from file or standard input , Then modify the contents according to the command list , Finally, send the results to the standard output .
The meaning of some common options :
-
-n Use silence (silent) Pattern . In general sed In the use of , All from STDIN The information about is usually listed on the screen . But if you add -n After the parameter , Only through sed The line of special treatment ( Or action ) To be listed . -
-e Directly in command line mode sed Action editor . -
-f Direct will sed The action is written in a file , adopt -f Parameters execute actions in the file . -
-i Directly modify the contents of the read file , Instead of printing to standard output .
Some common commands :
-
a newly added -
d Delete -
s Replace -
p Print
Delete the contents of the file
adopt d Parameters plus matching rules , We can easily delete the contents of the file .
$ sed '3d' fileName.txt # Delete the first 3 That's ok , And print the modification to the standard output
$ sed -i '3d' fileName.txt # Delete the first 3 That's ok , And take effect in the document , The following command will not add this parameter
# Delete last line
$ sed '$d' fileName.txt
# Delete 7-9 That's ok
$ sed '7,9d' fileName.txt
# Delete all blank lines
$ sed '/^$/d' fileName.txt
$ sed '/./!d' fileName.txt
# Delete the line that matches the string
$ sed '/awk/d' fileName.txt
# Delete all rows between two matches
$ sed '/patternA/,/patternB/d' fileName.txt
# Delete the matching row to n(5) Between lines , In the same way, it can be reversed , Delete the content from a row to the matching row
$ sed '/pattern/,5d' fileName.txt
# Delete rows that meet multiple criteria
$ sed '/patternA/d;/patternB/d' fileName.txt
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
Replace the contents of the file
If you just want to modify part of a line , have access to g Parameters , Specific examples are as follows .
$ sed 's/^/ABC /' fileName.txt # Add in each line ABC
$ sed 's/patternA/String/' fileName.txt # Replace the matching content with the specified string
- 1.
- 2.
- 3.
Although the container 、 Cloud infrastructure is very common today , In the enterprise operation and maintenance scenario, we directly use sed The chances of command are diminishing . but sed It's like a secret weapon in individual combat , If we can master , We will be able to keep our hands on food and not panic , Take this opportunity to show your boss , Then your future will be greatly improved .
Reference material :
- sed delte file lines
- SED introduction
- sed Delete content from the text
边栏推荐
- 【先收藏,早晚用得到】100个Flink高频面试题系列(一)
- Online excel file parsing and conversion to JSON format
- SISO decoder for a general (n,n-1) SPC code(補充章節3)
- 谈谈远程工作 | 社区征文
- Comparison of mongoose in express, KOA and egg
- After class, I looked at the document and went back to the lab. I picked up the forgotten SQL operators again
- 【无标题】
- 任意用户密码重置的10种方式
- File class learning
- Ffmpeg parity field frame interlace progressive command and code processing
猜你喜欢

Ffmpeg hardware codec NVIDIA GPU

【C】 Compilation preprocessing and environment

How ZABBIX can customize MySQL monitoring items and trigger alarms

神经网络与深度学习-2- 机器学习简单示例-PyTorch
![[collect first and use it sooner or later] 100 Flink high-frequency interview questions series (II)](/img/cf/44b3983dd5d5f7b92d90d918215908.png)
[collect first and use it sooner or later] 100 Flink high-frequency interview questions series (II)

网络安全威胁情报体系

Seeing the sudden death of a 28 year old employee, I was silent

Understanding of distributed transactions

Ffmpeg parity field frame interlace progressive command and code processing

SISO Decoder for Repetition(补充章节4)
随机推荐
6-6 batch sum (*)
7-2 h0107. Pig-Latin
vulhub
av_ read_ The return value of frame is -5 input/output error
網絡安全威脅情報體系
Class question: how to ensure that line table storage can be inserted at any time?
【先收藏,早晚用得到】100个Flink高频面试题系列(二)
SISO decoder for a general (n,n-1) SPC code(补充章节3)
[collect first and use it sooner or later] 49 Flink high-frequency interview questions series (I)
Global and Chinese markets for private internet access 2022-2028: Research Report on technology, participants, trends, market size and share
mariadb spider分片引擎初体验
Explain AI accelerators in detail: GPU, DPU, IPU, TPU... There are infinite possibilities for AI acceleration schemes
【C】 Compilation preprocessing and environment
【C】 ATOI function implementation +offsetof implementation + exchange binary odd and even digits
PIL-Pillow图像处理【1】-安装与新建
Spring 2021 daily question [week6 not finished]
送给大模型的「高考」卷:442人联名论文给大模型提出204个任务,谷歌领衔
GB gb28181 protocol video platform easygbs adds or deletes offline channels
6-5 count the number of words (file) (*)
Getting started with Wireshark