gpt4 book ai didi

regex - 按行号向量拆分文件

转载 作者:行者123 更新时间:2023-12-04 16:16:50 25 4
gpt4 key购买 nike

我有一个大文件,大约 10GB。我有一个行号向量,我想用它来拆分文件。理想情况下,我想使用命令行实用程序来完成此操作。作为正则表达式:

文件:

1 2 3 
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18

行号向量:

2 5

期望的输出:

文件 1:

1 2 3 

文件 2:

4 5 6
7 8 9
10 11 12

文件 3:

13 14 15
16 17 18

最佳答案

使用 awk:

$ awk -v v="2 5" '       # space-separated vector if indexes
BEGIN {
n=split(v,t) # reshape vector to a hash
for(i=1;i<=n;i++)
a[t[i]]
i=1 # filename index
}
{
if(NR in a) { # file record counter in the vector
close("file" i) # close previous file
i++ # increase filename index
}
print > ("file" i) # output to file
}' file

示例输出:

$ cat file2
4 5 6
7 8 9
10 11 12

关于regex - 按行号向量拆分文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62953828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com