Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opposite of Linux Split

Tags:

shell

split

I have a huge file and I split the big file into several small chunks and divide and conquer. Now I have a folder contains a list of files like below:

output_aa #(the output file done: cat input_aa | python parse.py > output_aa)
output_ab
output_ac
output_ad
...

I am wondering is there a way to merge those files back together FOLLOWING THE INDEX ORDER:

I know I could do it by using

cat * > output.all 

but I am more curious another magical command already exist comes with split..

like image 598
B.Mr.W. Avatar asked Aug 22 '13 15:08

B.Mr.W.


2 Answers

The magic command would be:

cat output_* > output.all

There is no need to sort the file names as the shell already does it (*).

As its name suggests, cat original design was precisely to conCATenate files which is basically the opposite of split.

(*) Edit:

Should you use an (hypothetical ?) locale that use a collating order where the a-z order is not abcdefghijklmnopqrstuvwxyz, here is one way to overcome the issue:

LC_ALL=C "sh -c cat output_* > output.all"
like image 174
jlliagre Avatar answered Oct 22 '22 18:10

jlliagre


There are other ways to concat files together, but there is no magical "opposite of split" in "linux".

Of course, talking about "linux" in general is a bit far fetched, as many distributions have different tools (most of them use a different shell already by default, like sh, bash, csh, zsh, ksh, ...), but if you're talking about debian based linux at least, I don't know of any distribution which would provide such a tool.


For sorting you can use the linux command "sort" ;

Also be aware that using ">" for redirecting stdout will override maybe existing contents, while ">>" will concat to an existing file.


I don't want to copycat, but still make this answer complete, so what jlliagre said about the cat command should also be considered of course (that "cat" was made to con-"cat" files, effectively making it possible to reverse the split command - but that's only provided you use the same ordering of files, so it's not exactly the "opposite of split", but will work that way in close to 100% of the cases (see comments under jlliagre answer for specifics))

like image 1
griffin Avatar answered Oct 22 '22 17:10

griffin