我有 100 个文本文件,每个文件大约 1gb 大小,都在同一个文件夹中 每个文件都包含许多由 block 分隔的信息,如转储的 sql 数据库文件
每个 block 都以字符串“Create Table `”开头 每个 block 以字符串“Table structure for table”结尾
因为我在 Linux 方面有经验,所以我想问你如何在 Linux 中编写脚本,该脚本从文件夹中的所有文件循环,为每个文件按 block 拆分此文件,并将这些 block 保存到以每个 block 命名的单独的 txt 文件中起始字符串,所有每个文件 block 都放在不同的文件夹中,以文件名命名。
For example, logic must be such as
For each file in folder
Create xFolder Named as file
For each match by regex((Create Table `).*(Table structure for table))
Create file in xFolder named as regex((Create Table `).*`) (Extract name between ` `` from this match)
Put matched text to file
Next match
Next file
所以如果文件 MyFirstFile.txt 包含 3 个 block :
Create Table `Table1` (
text
text
...
text )
- Table structure for table
Create Table `Table2` (
text
text
...
text )
- Table structure for table
Create Table `Table3` (
text
text
...
text )
- Table structure for table
然后必须创建名为 MyFirstFile 的文件夹 该文件夹下有3个txt文件Table1.txt、Table2.txt、Table3.txt
Table1.txt must have inside
Create Table `Table1` (
text
text
...
text )
- Table structure for table
Table2.txt must have inside
Create Table `Table2` (
text
text
...
text )
- Table structure for table
Table3.txt must have inside
Create Table `Table3` (
text
text
...
text )
- Table structure for table
最佳答案
以一个文件(file1.txt)为例:
for table_name in `grep "Create Table" file1.txt | awk '{print $3}'`
do
awk '/Create Table '"$table_name"'/{print;while(getline)if($0 !~/Create Table/)print;else exit}` file1.txt > $table_name.txt
done
对于所有文件,只需再循环一次。自己试试吧。
祝你好运!
关于regex - Linux 将多个文本文件解析成单独的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23856930/