# nl datafile
1 northwest NW Charles Main 3.0 .98 3 34
2 western WE Sharon Gray 5.3 .97 5 23
3 southwest SW Lewis Dalsass 2.7 .8 2 18
4 southern SO Suan Chin 5.1 .95 4 15
5 southeast SE Patricia Hemenway 4.0 .7 4 17
6 eastern EA TB Savage 4.4 .84 5 20
7 northeast NE AM Main Jr. 5.1 .94 3 13
8 north NO Margot Weber 4.5 .89 5 9
9 central CT Ann Stephens 5.7 .94 5 13
# cat datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
# more datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
datafile: END
#
# sed '/^eastern/i\
> new line begin\
> ***************' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
new line begin
***************
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
# sed '/eastern/{n;s/AM/Archie/;}' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE Archie Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
# sed -e '/northeast/h' -e '$g' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
northeast NE AM Main Jr. 5.1 .94 3 13
# sed -e '/northeast/h' -e '/CT/{g;}' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
northeast NE AM Main Jr. 5.1 .94 3 13
# sed -e '/northeast/h' -e '/CT/{G;}' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
northeast NE AM Main Jr. 5.1 .94 3 13
# sed -e '/northeast/h' -e '$g' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
northeast NE AM Main Jr. 5.1 .94 3 13
# sed -e '/northeast/h' -e '$G' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
northeast NE AM Main Jr. 5.1 .94 3 13
# nl datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23 23
southwest SW Lewis Dalsass 2.7 .8 2 18 18
southern SO Suan Chin 5.1 .95 4 15 15
southeast SE Patricia Hemenway 4.0 .7 4 17
17
eastern EA TB Savage 4.4 .84 5 20 20
northeast NE AM Main Jr. 5.1 .94 3 13 13
north NO Margot Weber 4.5 .89 5 9 9
central CT Ann Stephens 5.7 .94 5 13 13
#vi sedding2
1 /western/,/southeast/{
2 /^ *$/d
3 /Suan/{h;d;}
}
4 /Ann/g
5 s/TB \(Savage\)/Thomas \1/
注:2删除空行
4匹配模式Ann,g将暂存缓冲区内的行复制到模式缓冲区里,从而覆盖模式缓冲区的
内容
# sed -f sedding2 datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA Thomas Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
southern SO Suan Chin 5.1 .95 4 15
# sed 's/...$//' datafile
northwest NW Charles Main 3.0 .98 3
western WE Sharon Gray 5.3 .97 5
southwest SW Lewis Dalsass 2.7 .8 2
southern SO Suan Chin 5.1 .95 4
southeast SE Patricia Hemenway 4.0 .7 4
eastern EA TB Savage 4.4 .84 5
northeast NE AM Main Jr. 5.1 .94 3
north NO Margot Weber 4.5 .89
central CT Ann Stephens 5.7 .94 5
# sed 's/....//' datafile
hwest NW Charles Main 3.0 .98 3 34
ern WE Sharon Gray 5.3 .97 5 23
hwest SW Lewis Dalsass 2.7 .8 2 18
hern SO Suan Chin 5.1 .95 4 15
heast SE Patricia Hemenway 4.0 .7 4 17
ern EA TB Savage 4.4 .84 5 20
heast NE AM Main Jr. 5.1 .94 3 13
h NO Margot Weber 4.5 .89 5 9
ral CT Ann Stephens 5.7 .94 5 13
# sed 's/^....//' datafile
hwest NW Charles Main 3.0 .98 3 34
ern WE Sharon Gray 5.3 .97 5 23
hwest SW Lewis Dalsass 2.7 .8 2 18
hern SO Suan Chin 5.1 .95 4 15
heast SE Patricia Hemenway 4.0 .7 4 17
ern EA TB Savage 4.4 .84 5 20
heast NE AM Main Jr. 5.1 .94 3 13
h NO Margot Weber 4.5 .89 5 9
ral CT Ann Stephens 5.7 .94 5 13