[Linux Basics] Introduction to the use of sed

[Linux Basics] Introduction to the use of sed

foreword

	主要介绍sed的基础用法,便于后续使用过程中进行查阅。

Introduction to Sed

	`sed` 是 Linux 和其他类 Unix 操作系统上的一个非常常用的文本处理工具,它的名字来自于 "流编辑器"(Stream Editor)的缩写。`sed` 可以通过命令行参数、正则表达式和命令来处理文本数据,包括文本替换、删除、插入、截取等操作,它可以用于各种文本处理任务,如文本转换、数据抽取、格式化输出等。

	`sed` 通常用于处理文本数据流,它会逐行读取输入文件,并根据命令对每一行进行处理,然后输出结果。`sed` 命令的输出可以通过重定向到文件或管道传递到其他工具中进一步处理或分析。

Use of Sed

grammatical format

格式:
	sed [options] command [input_file...]
  • optionsis an optional command-line option;
  • commandis sedthe command ;
  • input_fileis the input file to process. If not specified input_file, data is read from standard input by default.

Commonly used [options] of sed

-n:禁止默认输出,只输出经过命令处理的行。
-e:允许多个命令进行处理。
-f:从文件中读取命令。
-i:直接修改文件内容。
-r:启用扩展正则表达式。
-s:处理多行文本。
  • options option (not commonly used):
-u:使用非缓冲模式输出结果。
-z:使用 null 字符而不是换行符作为行分隔符。
-b:将备份文件的扩展名添加到备份文件中。
-B:设置备份文件的备份级别。
-E:启用 POSIX 扩展正则表达式。
-H:启用文件名打印。
-h:禁用文件名打印。
-l:启用离线操作。
-n:禁止默认输出。
-r:启用扩展正则表达式。
-s:处理多行文本。
-u:使用非缓冲模式输出结果。
-w:强制 sed 以原子方式写入文件。
-a:设置输出文件的访问时间为输入文件的访问时间。
-c:在命令模式下,将多个 sed 命令组合成一个字符串。
-l:启用对行末的空格的处理。
-q:在第一次匹配成功后退出。
-R:启用反向引用。
-T:禁用 tab 字符的展开

Sed common command parameters

a:在当前行后添加新行。
c:替换当前行。
d:删除当前行。
i:在当前行前插入新行。
p:打印当前行。
s:替换匹配的文本。
y:替换字符。
  • command parameter (not commonly used)
b:分支到脚本中的标签命令。
e:允许对模式空间使用多行脚本。
f:从文件中读取脚本。
g:替换字符串中所有的匹配项。
h:将模式空间复制到保持空间。
H:将模式空间追加到保持空间。
l:将控制字符输出为可见字符。
n:读取下一行,但不输出当前行。
N:将下一行追加到模式空间中。
P:打印模式空间中的第一行。
q:退出 sed。
r:从文件中读取内容并将其追加到模式空间中。
t:分支到脚本中的标签命令,如果 s 命令替换了文本,则分支。
T:分支到脚本中的标签命令,如果 s 命令未替换任何文本,则分支。
w:将模式空间中的内容写入文件。
x:交换模式空间和保持空间中的内容。
::用于标签定义和无操作。

sed example

  • *** S replace string ***
  1. Replaces all old_stringoccurrences new_stringwith and outputs the result to the specified output file. gThe option here means global replacement.
sed 's/old_string/new_string/g' input_file > output_file
  1. will find old_stringand new_string.
sed '1,10s/old_string/new_string/g' input_file > output_file
  1. will find all occurrences of between matches start_patternand and replace them with .end_patternold_stringnew_string
sed '/start_pattern/,/end_pattern/s/old_string/new_string/g' input_file > output_file
  1. will find all occurrences of in the input file old_stringand replace them with new_string, then delete all lines patternthat .
sed 's/old_string/new_string/g; /pattern/d' input_file > output_file
  • *** DELETE LINE ***
  1. Deletes all patternmatching and outputs the result to the specified output file.
sed '/pattern/d' input_file > output_file
  1. Delete the specified line (delete the third line of text)
sed '3d' file.txt
  • *** INSERT NEW ROW ***
  1. will insert a new line of text before nline new_lineand output the result to the specified output file.
sed 'n i\new_line' input_file > output_file
  • *** Add new line after matching line ***
  1. A new line of text will be added after the line patternmatched new_lineby and output to the specified output file.
sed '/pattern/a\new_line' input_file > output_file
  • Add the specified string at the beginning and end of the specified line
  1. Add a # sign at the beginning of the specified line
sed 'n s/^/#/' input_file > output_file
  1. Add a # sign at the end of the specified line
sed 'n s/$/#/' input_file > output_file
  1. Add at the beginning or end of all
sed  's/^/#/' input_file > output_file
sed  's/¥/#/' input_file > output_file

Guess you like

Origin blog.csdn.net/qq_43714097/article/details/129908270