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

时间:2016-03-18 16:16:20

标签: 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

2 个答案:

答案 0 :(得分:2)

以下内容确保不会同时打开太多文件句柄。应该用适当的表达式替换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

答案 1 :(得分:1)

试试这个:

/*
 * contentScriptWhen: "start"
 *
 * "start": Load content scripts immediately after the document
 * element is inserted into the DOM, but before the DOM content
 * itself has been loaded
 */

/*
 * use an empty HTMLElement both as a place_holder
 * and a way to prevent the DOM content from loading
 */
document.replaceChild(
        document.createElement("html"), document.children[0]);
var rqst = new XMLHttpRequest();
rqst.open("GET", document.URL);
rqst.responseType = 'document';
rqst.onload = function(){
    if(this.status == 200) {
        /* edit the document */
        this.response.children[0].children[1].querySelector(
                "#content-load + div + script").remove();

        /* replace the place_holder */
        document.replaceChild(
                document.adoptNode(
                    this.response.children[0]),
                document.children[0]);

        // use_the_new_world();
    }
};
rqst.send();

处理当前目录中的所有文件。

如果您只想在第一部分匹配多个文件时生成新文件:

for f in *.*; do
  cat "${f}" >> "${f%.*}" 
done