linux - 根据 Linux 中的名称将文件内容连接到一个文件中

标签 linux awk sed

我想在名称的第一部分匹配时将文件连接成一个文件,例如,如果我有以下文件:

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/

相关文章:

linux - Grep\+ 符号后的第一个(2 位数)数字

bash - 删除等号后的多行字符串

android - 从命令行删除 Android SDK 包

Linux cronjob wget 到文件而不是 STDOUT

Linux命令行如何接受没有pin的蓝牙设备配对

linux - 从linux中的文件中提取字符串下的字符串

regex - 句子中的 Sed 或 Awk 或 Perl 替换

linux - 如何让bash for循环退出?

sed - 将匹配的行复制到第二个文件

bash - 有选择地从 'iptables -nvL' 命令输出中删除不必要的列空格吗?