作业帮 > 综合 > 作业

sed文件存储的问题有文件file如下:1txt-------------------some textsome oth

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/06 11:43:05
sed文件存储的问题
有文件file如下:
1txt
-------------------
some text
some other text
2txt
-----------------------------
some text2
some other text2
3txt
------------------------
some text 3
some other text 3
用sed 怎样把这个文件分割,存为1txt,2txt和2txt
awk比较好做
如果每个都是定长为5行
awk '{if(NR%5==0) {print a[1]"\n"a[2]"\n"a[3]"\n"a[4]"\n"a[0]>>a[1];} else {a[NR%5]=$0;}}' file
如果不是定长
awk '{if($0 ~/txt/) {filename=$0;a=$0;} else if($0 ~/^$/) print a"\n">>filename; else a=a"\n"$0;}' file
file最后要有一个空行