如何将我的文件分割成多个文件? [英] How can I split my file into multiple files?
本文介绍了如何将我的文件分割成多个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想文件分割成多个文件。我输入
I want to split a file into multiple files. My input is
Report : 1
ABC
DEF
GHI
JKL
End of Report
$
Report : 2
ABC
DEF
GHI
JKL
$
Report : 2
ABC
DEF
GHI
JKL
End of Report
$
Report : 3
ABC
DEF
GHI
JKL
End of Report
$
输出应该是:
文件1
Report : 1
ABC
DEF
GHI
JKL
End of Report
$
文件2
Report : 2
ABC
DEF
GHI
JKL
$
Report : 2
ABC
DEF
GHI
JKL
End of Report
$
文件3
Report : 3
ABC
DEF
GHI
JKL
End of Report
$
我曾尝试
awk '{print $0 "Report :"> "/tmp/File" NR}' RS="END OF" test.txt
但我没有得到适当的输出。
but I'm not getting appropriate output.
任何指导,将AP preciated。
Any guidance would be appreciated.
推荐答案
下面是另一个awk的回答是:
Here's another awk answer:
awk '/^Report/{n=$3} {print > "File"n}' input
这是类似于nu11p01n73R的答案,但使用每个报告
行的第三个字段,以确定文件数量。
This is similar to nu11p01n73R's answer but uses the third field of each Report
line to determine the file number.
- 当
/ ^报告/
线,集N
来匹配$ 3
。 - 使用
n制作的文件名每一行打印到何时
- When
/^Report/
matches the line, the setn
to$3
. - Use
n
when creating the file name to print each line to
如果你有大量的这些块,你可能需要最终关闭文件和可以使用这个命令:
If you have a large number of these blocks, you might need to end up closing files and could use this command instead:
awk '/^Report/{f="File"$3; if(lf != f) {close(lf); lf=f}} {print > f}' input
- 当
/ ^报告/
行,创建一个文件名匹配˚F
。 - 如果
LF
(最后一个文件名)不匹配˚F
,先尝试关闭LF
然后重置LF
。调用close()时,LF
尚未设置安全 - 打印每行
˚F
- When
/^Report/
matches the line, create a filenamef
. - If
lf
(last filename) doesn't matchf
, first try to cloself
then resetlf
. Calling close() whenlf
hasn't been set is safe - print every line to
f
这篇关于如何将我的文件分割成多个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文