Bash sed 读和写文件入门

Bash sed 读和写文件入门

一、read

 

‘[position line]r source.txt’ target.txt

 

Find the line from target.txt through position line, and then read all lines from source.txt, at last put all lines after the line position in target.txt.

 

If the position line is not found, just put out target.txt

 

If position line is not supplied, put all lines in source.txt after every line of the target.txt.

 

 

[braveyly@m-net ~]$ cat source.txt

 

a

 

b

 

c

 

[braveyly@m-net ~]$ cat target.txt

 

1

 

2

 

3

 

[braveyly@m-net ~]$ sed '/2/r target.txt' source.txt

 

a

 

b

 

c

 

[braveyly@m-net ~]$ sed '/2/r source.txt' target.txt

 

1

 

2

 

a

 

b

 

c

 

3

 

[braveyly@m-net ~]$ sed 'r source.txt' target.txt

 

1

 

a

 

b

 

c

 

2

 

a

 

b

 

c

 

3

 

a

 

b

 

c

[braveyly@m-net ~]$ sed '1r source.txt' target.txt

 

1

 

a

 

b

 

c

 

2

 

3

 

 

二、write

‘[position line]w target.txt’ source.txt

 

Read lines from source.txt through position line. And then put out all these lines into target.txt.

If the target.txt file is not existed, then create it.

 

[braveyly@m-net ~]$ cat meeting.txt

 

9:00    VoIP    LING       2

 

10:00   IAD     MEAT       2

 

9:12    VoIP    TYU        3

 

12:00   VoIP    QWE        4

[braveyly@m-net ~]$ sed -e '/VoIP/w VoIP.txt' ./meeting.txt

 

9:00    VoIP    LING       2

 

10:00   IAD     MEAT       2

 

9:12    VoIP    TYU        3

 

12:00   VoIP    QWE        4

 

[braveyly@m-net ~]$ cat VoIP.txt

 

9:00    VoIP    LING       2

 

9:12    VoIP    TYU        3

 

12:00   VoIP    QWE        4

 

[braveyly@m-net ~]$ sed -e '/IAD/w IAD.txt' ./meeting.txt

 

9:00    VoIP    LING       2

 

10:00   IAD     MEAT       2

 

9:12    VoIP    TYU        3

 

12:00   VoIP    QWE        4

 

[braveyly@m-net ~]$ cat IAD.txt

 

10:00   IAD     MEAT       2

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值