deltanovember
deltanovember

Reputation: 44051

In Linux, if I have a bunch of text files is there a simple script to merge them into one file?

For example suppose I have 1.csv, 2.csv, ... , 20.csv. Is there a simple shell script where I can merge everything into merged.csv?

Upvotes: 2

Views: 396

Answers (4)

PhilR
PhilR

Reputation: 5592

cat {1..20}.csv > merged.csv

It's what cat was made for! Note that I've used a bash-ism in the {1..20} sequence.

Upvotes: 0

John Kugelman
John Kugelman

Reputation: 361595

Use cat to concatenate them.

cat *.csv > merged.csv

As @sarnold points out this will concatenate them out of order. If that's important, use his for loop suggestion, or this xargs pipeline:

ls *.csv | sort -n | xargs cat > merged.csv

Upvotes: 6

Mark Drago
Mark Drago

Reputation: 2058

If you're looking to merge files by appending the lines from one on to the end of the lines of the other, then you're looking for paste.

For example, if file1 contains:

1,One
2,Two

And file2 contains:

A,B,C
D,E,F

You could run paste -d , file1 file2 to get this:

1,One,A,B,C
2,Two,D,E,F

Upvotes: 2

sarnold
sarnold

Reputation: 104050

The simple answer:

cat *.csv > merged.csv

will sort 1.csv 10.csv 2.csv 20.csv 3.csv.... If you want them sorted by number, it takes a bit more work:

for i in `seq 1 20`; do cat ${i}.csv >> merged.csv ; done

Upvotes: 1

Related Questions