我想将文件拆分成多个文件.我的意见是
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
$
Run Code Online (Sandbox Code Playgroud)
输出应该是:
档案1
Report : 1
ABC
DEF
GHI
JKL
End of Report
$
Run Code Online (Sandbox Code Playgroud)
档案2
Report : 2
ABC
DEF
GHI
JKL
$
Report : 2
ABC
DEF
GHI
JKL
End of Report
$
Run Code Online (Sandbox Code Playgroud)
档案3
Report : 3
ABC
DEF
GHI
JKL
End of Report
$
Run Code Online (Sandbox Code Playgroud)
我试过了
awk '{print $0 "Report :"> "/tmp/File" NR}' RS="END OF" test.txt
Run Code Online (Sandbox Code Playgroud)
但我没有得到合适的输出.
任何指导将不胜感激.
你可以尝试类似的东西
$awk '/^Report/{filename++} {print > "FILE"filename}' input
Run Code Online (Sandbox Code Playgroud)
测试
$awk '/^Report/{filename++} {print > "FILE"filename}' input
$ cat FILE1
Report : 1
ABC
DEF
GHI
JKL
End of Report
$
$ cat FILE2
Report : 2
ABC
DEF
GHI
JKL
$
Report : 2
ABC
DEF
GHI
JKL
End of Report
$
$ cat FILE3
Report : 3
ABC
DEF
GHI
JKL
End of Report
$
Run Code Online (Sandbox Code Playgroud)
它能做什么
/^Report/对于Report以同一行中第三列中的数字开头的行,模式为true,该文件名必须用作下几行的文件名
{filename++} 将文件名值增加1
{print > "FILE"filename} 将每一行打印到文件中.
例如,如果filename是,1那么这条线是相同的
print > FILE1
Run Code Online (Sandbox Code Playgroud)
这是输出重定向,与bash等中使用的重定向相同.
请注意,print如果遗漏了属性,则没有属性,然后awk打印整个记录.这就像写作一样print $0 > "FILE"filename
试试这个,
csplit input.txt '/End of Report$/' '{*}'
Run Code Online (Sandbox Code Playgroud)
csplit 是一个UNIX实用程序,用于将文件拆分为由上下文行确定的两个或多个较小的文件.
input.txt 这是将被分割的文件.
'/End of Report$/' 特定模式,如"报告结束".
'{*}' 选项,指示整个文件.
| 归档时间: |
|
| 查看次数: |
232 次 |
| 最近记录: |