我想在名称的第一部分匹配时将文件连接成一个文件,例如,如果我有以下文件:
file1.name1 which contains :
this is file1
file1.name2 which contains :
this is file2
file2.name3 which contains :
this is file1
file2.name4 which contains :
this is file2
结果是这样的
file1 will contain
this is file1
this is file2
file2 will contain
this is file1
this is file2
最佳答案
以下确保不会同时打开太多文件句柄。 PATHNAMES 应替换为适当的表达式,该表达式产生要处理的文件的路径名。
警告:如果更改了预先存在的文件,则不会发出警告。
awk -v maxhandles=10 '
{ nparts=split(FILENAME,a,".");
# if FILENAME does not match the pattern:
if (nparts <= 1) { print; next }
# n is the number of open handles:
if (! (a[1] in handles)) {
n++;
if (n > maxhandles) {
for (i in handles) { close(i) };
n=0;
delete handles;
}
}
handles[a[1]];
print >> a[1]
}
' PATHNAMES
关于linux - 根据 Linux 中的名称将文件内容连接到一个文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36089287/