当前位置:网站首页>Shell script text three swordsmen sed
Shell script text three swordsmen sed
2022-07-27 11:46:00 【Brother frog who doesn't look back】
Catalog
1 sed Tools
1.1 sed summary
sed(Stream EDitor) Is a powerful and simple text analysis conversion tool , Can read text , And edit the text content according to the specified conditions ( Delete 、 Replace 、 add to 、 Mobile, etc ), Finally, output all lines or only some lines of processing .sed It can also realize quite complex text processing operation without interaction , Widely used in Shell Script , It is used to complete various automatic processing tasks .
1.2 sed workflow
sed The workflow of mainly includes reading 、 Perform and display three processes .
- Read :sed From the input stream ( file 、 The Conduit 、 The standard input ) Read a line in and store it in a temporary buffer ( Also called pattern space ,pattern space).
- perform : By default , be-all sed Commands are executed sequentially in pattern space , Unless the address of the line is specified , otherwise sed The command will be executed on all lines in turn .
- Show : Send the modified content to the output stream . After sending the data , Mode space will be cleared .
Before all the contents of the file are processed , The above process will be repeated , Until all content is processed .

Be careful : By default, all sed Commands are executed in pattern space , So the input file doesn't change , Unless you're using redirection to store output .
1.3 sed Command common usage
Usually called sed There are two formats for commands , As shown below . among ,“ Parameters ” Refers to the target file of the operation , Use when there are multiple operands , Comma between files “,” Separate ; and scriptfile Represents a script file , Need to use “-f” Option assignment , When the script file appears before the target file , Indicates that the input target file is processed through the specified script file .
Format : sed [ Options ] ' operation ' Parameters
sed [ Options ] -f scriptfile Parameters
| common sed Command options | |
| -e or --expression= | Indicates that the input text file is processed with instructions, commands or scripts |
| -f or --file= | Indicates that the input text file is processed with the specified script file |
| -h or --help | Display help |
| -n、--quiet or silent | Show only processed results |
| -i.bak | Edit text file directly |
| -r、-E | Using extended regular expressions |
| -s | Treat multiple files as separate files , Instead of a single continuous long file stream |
“ operation ” Used to specify the action behavior of file operation , That is to say sed The order of . It is usually used “[n1[,n2]]” Format of operation parameters .n1、n2 It's optional , Represents the number of rows selected for operation , If the operation needs to be in 5~ 20 Between lines , Is represented as “5,20 Action action ”.
| Common operations | |
| a | increase , Add a line below the current line to specify |
| c | Replace , Replace the selected row with the specified content |
| d | Delete , Delete selected rows |
| i | Insert , Insert a row above the selected row to specify |
| p | Print , If you also specify a row , Indicates to print the specified line ; If no line is specified , It means printing everything ; If there are non printing characters , with ASCII Code output . It is usually associated with “-n” Use options together . |
| s | Replace , Replace specified characters |
| y | Character conversion |
2 example
Test text
1 He was short and fat.
2 He was wearing a blue polo shirt with black pants.
3 The home of Football on BBC Sport online.
4 the tongue is boneless but it breaks bones.12!
5 google is the best tools for search keyword.
6 The year ahead will test our political establishment to the limit.
7 PI=3.141592653589793238462643383249901429
8 a wood cross!
9 Actions speak louder than words
10 #woood #
11 #woooooood
12 # AxyzxyzxyzxyzC
13 I bet this place is really spooky late at night!
14 Misfortunes never come alone/single.
example 1





sed When a command is combined with a regular expression , Slightly different format , Regular expression to “/” Surround .
example 1


example 2: Delete eligible text (d)





example 3: Replace eligible text
In the use of sed Command to replace the operation needs to use s( String substitution )、c( The whole line / Replace the whole piece )、y( Character conversion ) Command options







example 4: Migrate eligible text
In the use of sed When the command migrates qualified text , The following parameters are commonly used .
- H: Copy to clipboard
- g、G: Overlay the data in the clipboard / Append to specified line
- w: Save as a file
- r: Read the specified file
- a: Add the specified content . The specific operation method is as follows
- I,i Ignore case






example 5 : Editing files with scripts
Use sed The script stores multiple editing instructions in a file ( One editing instruction per line ), adopt “-f” Option to call . For example, execute the following command to convert the second 1~6 Line content transferred to 16 After line .


边栏推荐
- USB 网卡驱动数据流
- 数据包传输:应用层-内核-硬件
- 剑指 Offer 笔记: T58 - II. 左旋转字符串
- 基于反馈率的控制系统原理
- Keil MDK编译出现..\USER\stm32f10x.h(428): error: #67: expected a “}“错误的解决办法
- 微博评论爬虫+可视化
- Installation and use of GTEST and gmock
- Detailed explanation of MATLAB S-function
- The C programming language 2nd -- Notes -- 6.7
- LeetCode 03: T58. 最后一个单词的长度(简单); 剑指 Offer 05. 替换空格(简单); 剑指 Offer 58 - II. 左旋转字符串(简单)
猜你喜欢

美现首例孕妇猴痘病例:新生儿被注射免疫球蛋白,已安全出生

Beyond compare 3 next difference segment / down search arrow not found
![[machine learning whiteboard derivation series] learning notes - probability graph model and exponential family distribution](/img/cc/a27db6c5a380102029b85255df79c9.png)
[machine learning whiteboard derivation series] learning notes - probability graph model and exponential family distribution

SMA TE: Semi-Supervised Spatio-Temporal RepresentationLearning on Multivariate Time Series

Adobe audit prompts that the sampling rate of audio input does not match the output device - problem solving

Moveit2 -- 2. Quick start of moveit in rviz

Vscode removes style / syntax highlighting / code highlighting / black background when copying code

N ¨UWA: Visual Synthesis Pre-training for Neural visUal World creAtionChenfei

Maker Hongmeng application development training 04

What is private traffic?
随机推荐
Maker harmony OS application development training notes 01
Adobe Audition提示 音频输入的采样率与输出设备不匹配——问题解决
Maker Hongmeng application development training 04
compute_class_weight() takes 1 positional argument but 3 were given
为什么选择智能电视?
剑指 Offer 笔记: T57 - II. 和为 s 的连续正数序列
希腊字母读法
求不同采样周期下的传递函数有限零点
美现首例孕妇猴痘病例:新生儿被注射免疫球蛋白,已安全出生
Maker Hongmeng application development training notes 03
Win10 vscode code code format setting and remote breakpoint debugging
C programming language (2nd Edition) -- Reading Notes -- 1.5.3
Summary of C language knowledge involved in learning STM32F103 (link only)
(9) Shell I / O redirection
C programming language (2nd Edition) -- Reading Notes -- 1.4
Several banks adjusted the redemption rules of cash management financial products: the confirmation time limit of redemption changed from "t+0" to "t+1"
Analysis of the use of JUC framework from runnable to callable to futuretask
开源flink有写holo的Datastream connector或者flink sql conn
Ask the big guys, is there transaction control for using flick sink data to MySQL? If at a checkpoint
多种进制之间的转换