joe5
joe5

Reputation: 1147

concatenate files with shared feature in the name

I want to concatenate files that share a unique identifier within the name.

my files look like this

A_1.file

A_2.file

A_3.file

B_1.file

B_2.file

B_3.file

etc...

What I want is something that will cat them into

A.file

B.file

But in reality I have hundreds of files, with much more complicated identifiers than "A" or "B". So just saying "cat A*.file > A.file" won't work because it would require manually typing each sequence identifier. So I guess that is essentially my problem, concatenating files with shared unique identifer. The ID of interest first in the file name and is seperated by an "_", so perhaps there is a good regex solution. I apologize if this has been addressed, everything I found was either different enough and overly complicated. I've just been fumbling around with this for too long.

thanks

Upvotes: 0

Views: 29

Answers (1)

Walter A
Walter A

Reputation: 19982

You can use

awk -F _ 'FNR==1 {out=$1".file"}  {print > out}' *_*

This way you avoid collecting the identifiers first and making commands for each found identifier.

-F _ : field sep
FNR==1 : first line of each file
out=$1".file" : construct output filename

Upvotes: 2

Related Questions