malmo
malmo

Reputation: 524

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

Upvotes: 0

Views: 68

Answers (2)

SLePort
SLePort

Reputation: 15461

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

Upvotes: 1

peak
peak

Reputation: 116850

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

Upvotes: 2

Related Questions