深入理解Sed
相关文章
简介
man sed
NAME
sed -- stream editor
SYNOPSIS
sed [-Ealn] command [file ...]
sed [-Ealn] [-e command] [-f command_file] [-i extension] [file ...]
DESCRIPTION
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 is a
stream editor
.
Benchmarks
bbs_list
each row is
a computer bulletin board
phone number
baud rate
a code
.
aardvark 555-5553 1200/300 B
alpo-net 555-3412 2400/1200/300 A
barfly 555-7685 1200/300 A
bites 555-1675 2400/1200/300 A
camelot 555-0542 300 C
core 555-2912 1200/300 C
fooey 555-1234 2400/1200/300 B
foot 555-6699 1200/300 B
macfoo 555-6480 1200/300 A
sdace 555-3430 2400/1200/300 A
sabafoo 555-2127 1200/300 C
inventory-shipped
each row is
year
green crates shipped
red boxes shipped
orange bags shipped
blue packages shipped
Jan 13 25 15 115
Feb 15 32 24 226
Mar 15 24 34 228
Apr 31 52 63 420
May 16 34 29 208
Jun 31 42 75 492
Jul 24 34 67 436
Aug 15 34 47 316
Sep 13 55 37 277
Oct 29 54 68 525
Nov 20 87 82 577
Dec 17 35 61 401
Jan 21 36 64 620
Feb 26 58 80 652
Mar 24 75 70 495
Apr 21 70 74 514
e1. replace 555 to 444 in bbs_list
sed 's/555/444/' bbs_list
aardvark 444-5553 1200/300 B
alpo-net 444-3412 2400/1200/300 A
barfly 444-7685 1200/300 A
bites 444-1675 2400/1200/300 A
camelot 444-0542 300 C
core 444-2912 1200/300 C
fooey 444-1234 2400/1200/300 B
foot 444-6699 1200/300 B
macfoo 444-6480 1200/300 A
sdace 444-3430 2400/1200/300 A
sabafoo 444-2127 1200/300 C
Sed Process Format
-
sed ‘program’ input-file1 input-file2
-
sed -f program-file input-file1 input-file2
Sed Examples | 解释 |
---|---|
sed ’s/hello/world/’ input.txt > output.txt | hello -> world 并输出到output.txt |
sed ’s/hello/world/’ < input.txt > output.txt | 同上 |
cat input.txt | sed ’s/hello/world/’ - > output.txt | input-file is - ,sed receive standard input |
sed -i ’s/hello/world/’ file.txt | use -i to process and edit file.txt |
Command-Line Opts
Opts | 说明 |
---|---|
-e 'program' | |
-f program-file | |
-i | 编辑当前文件 |
-l | Make output line buffered. |
-n | 不把处理过程输出到屏幕上 |
p with -n 输出特定行: | sed -n ’3p’ file.txt |
exit 0 | succ |
exit 1 | invalid command |
exit 2 | 多个文件处理其中一个文件打不开 |
exit 4 | IO error |
Program
sed
:[addr]X[options]
[addr]
:a single line number、a regular expression、a range of linesX
:action[options]
:additional opts
Opts | 说明 |
---|---|
sed ‘30,35d’ input.txt > output.txt | 30,35([addr] range of lines);d(delete) action |
sed ‘/^foo/q45’ input.txt > output.txt | /^foo/([addr] regular expression) ; q(quit);42(the opt of q) |
sed ‘/^foo/d ; s/hello/world/’ input.txt > output.txt | 删除开头为foo,替换hello为world |
Often-Used Commands
Commands | 说明 | Example |
---|---|---|
a\ text | 1.append text after a line 2.命令行输入\直接按回车即可 3.用script脚本,则换行即可 | sed '3a\ hello ’ bbs_list |
d | 删除选中的行 | seq 5 | sed ‘2,4d’ |
q[exit-code] | 匹配的addr处quit | seq 5 | sed ‘2q’ |
p | 和-n 一起使用打印对应行 | seq 5 | sed -n ‘3p’ |
n | skip lines | seq 8 | sed ‘n;n;s/./x/’ |
c | 替换选中行 | seq 8 | sed ‘2c\ hahaha ’ |
i | insert before select lines | seq 8 | sed ‘2i\ haa ’ |
s/regexp/replacement/flag | 1. s/v1/v2/g : apply the replace to all matches 2. s/v1/v2/num : the numth matches | 1. echo 1,1,1,1,1 | sed 's/1/2/g’ 2. echo 1,1,1,1,1 | sed ‘s/1/3/3’ |
小结
-
sed 常见于处理批量文本替换功能
-
相比较sed,awk的功能更加丰富,awk也一定程度上包含了sed的功能,不过awk常用处理明确列数的数据文件
个人简介
工作:Senior Engineer
Alibaba
email:sunquan9301@163.com
WX:sunquan97
HomePage:qsun97.com