- 论坛徽章:
- 0
|
请教:用sed实现文件分割功能
试试这个:
$cat filename
1 #include <sys/types.h>
2 #include <unistd.h>
3
4 int main(int argc, char **argv)
5 {
6 char ch = 0;
7
8 while (read(0, &ch, 1) == 1) {
9 if (ch < 0) {
10 if (read(0, &ch, 1) != 1)
11 break;
12 continue;
13 }
14 write(1, &ch, 1);
15 }
16 return 0;
17 }
程序如下:
nawk '{ line[NR] = $0 }
END { for(i = 1; i <= NR; i++){
if(i < 6)
print line > "fhead"
if(i >= 6 && i <= NR - 2)
print line > "fbody"
if( i > NR - 2)
print line > "ftail" }
}' filename
运行结果:
$cat fhead
1 #include <sys/types.h>
2 #include <unistd.h>
3
4 int main(int argc, char **argv)
5 {
$cat fbody
6 char ch = 0;
7
8 while (read(0, &ch, 1) == 1) {
9 if (ch < 0) {
10 if (read(0, &ch, 1) != 1)
11 break;
12 continue;
13 }
14 write(1, &ch, 1);
15 }
$cat ftail
16 return 0;
17 } |
|