简体   繁体   中英

Concatenate files content into one single file based on their name in linux

I want to concatenate files into one single file when the first part of their name match for example if I have the following files:

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

the result would be like

file1 will contain 
this is file1
this is file2
file2 will contain
this is file1
this is file2

The following ensures that not too many file handles are open at the same time. PATHNAMES should be replaced by an appropriate expression yielding the path names of the files to be processed.

WARNING: No warning is given if a pre-existing file is altered.

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

Try this :

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

All files with a dot in current dir are processed.

If you want to generate new file only if first part match multiple files :

files=(*)
for f in *.*; do
  numf=$(grep -o "${f%.*}" <<< "${files[*]}" | wc -l)
  [ $numf -gt 1 ] && cat "${f}" >> "${f%.*}"
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM