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 |