【Linux】深入理解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

  1. sed ‘program’ input-file1 input-file2

  2. sed -f program-file input-file1 input-file2

Sed Examples解释
sed ’s/hello/world/’ input.txt > output.txthello -> world 并输出到output.txt
sed ’s/hello/world/’ < input.txt > output.txt同上
cat input.txt | sed ’s/hello/world/’ - > output.txtinput-file is -,sed receive standard input
sed -i ’s/hello/world/’ file.txtuse -i to process and edit file.txt

Command-Line Opts

Opts说明
-e 'program'
-f program-file
-i编辑当前文件
-lMake output line buffered.
-n不把处理过程输出到屏幕上
p with -n 输出特定行:sed -n ’3p’ file.txt
exit 0succ
exit 1invalid command
exit 2多个文件处理其中一个文件打不开
exit 4IO error

Program

sed[addr]X[options]

  • [addr] :a single line number、a regular expression、a range of lines
  • X :action
  • [options] :additional opts
Opts说明
sed ‘30,35d’ input.txt > output.txt30,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处quitseq 5 | sed ‘2q’
p-n一起使用打印对应行seq 5 | sed -n ‘3p’
nskip linesseq 8 | sed ‘n;n;s/./x/’
c替换选中行seq 8 | sed ‘2c\
hahaha
iinsert before select linesseq 8 | sed ‘2i\
haa
s/regexp/replacement/flag1. 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’

小结

  1. sed 常见于处理批量文本替换功能

  2. 相比较sed,awk的功能更加丰富,awk也一定程度上包含了sed的功能,不过awk常用处理明确列数的数据文件

个人简介

工作:Senior Engineer Alibaba
email:sunquan9301@163.com
WX:sunquan97
HomePage:qsun97.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值