Enjolras
Enjolras

Reputation: 13

how to merge multiple text files using bash and preserving column order

I'm new to bash, I have a folder in which there are many text files among them there's a group which are named namefile-0, namefile-1,... namefile-100. I need to merge these file all in a new file. The format of each of these files is: header and 3 columns of data.

It is very important that the format of the new file is: 3 * 100 columns of data respecting the order of the columns (123123123...).

I don't mind if the header is also repeated or not.

I'm also willing, in case it was necessary, to place all these files in a folder in which no other files are present.

for i in {1..100}
do 
paste `echo "namefile$i"` >> `echo "b"
done 

which prints only the first file into b.

STR=""
for i in {1..100}
do
    STR=$STR"namefile"$i" "
done
paste $STR > b  

which prints everything but does not preserve the order of the columns.

Upvotes: 0

Views: 657

Answers (3)

Mihir Luthra
Mihir Luthra

Reputation: 6759

You need to mention what delimeter separates columns in your file.

Assuming the columns are separated by a single space,

paste -d' ' namefile-* > newfile

Other conditions like existence of other similar files or directories in the working directory, stripping of headers etc can also be tackled but some more information needs to be provided in the question.

Upvotes: 1

dwmorrin
dwmorrin

Reputation: 2734

paste namefile-{0..100} >combined

Upvotes: 1

michjnich
michjnich

Reputation: 3385

paste namefile* > new_file_name

Upvotes: 0

Related Questions