davidkomer
davidkomer

Reputation: 3078

Concatenate files with prefix

Assume a folder with files named like this:

FOO.1
FOO.2
...
BAR-1.1
BAR-1.2
...
BAR-2.1
BAR-2.2
...

And I would like to concatenate them such that it results in 3 files:

FOO (consisting of FOO.1 + FOO.2 + FOO.N)
BAR-1 (consisting of BAR-1.1 + BAR-1.2 + BAR-1.N)
BAR-2 (consisting of BAR-2.1 + BAR-2.2 + BAR-2.N)

How would this be done in bash/shell script? Assume all the files are in one folder (no need to go into subfolders)

Requires not knowing the filename prefixes in advance

Upvotes: 0

Views: 249

Answers (2)

RKG
RKG

Reputation: 150

for file in *.*
do
  prefix="${file%.*}"
  echo "Adding $file to $prefix ..."
  cat "$file" >> "$prefix"
done

Upvotes: 1

Farvardin
Farvardin

Reputation: 5424

for i in  $(ls | sed  's/\(.*\)\..$/\1/' | sort -u)
do
  cat $i* > $i
done

Upvotes: 0

Related Questions