Home > OS >  How to concatenate files split by round robin?
How to concatenate files split by round robin?

Time:01-10

I split a file using split -n r/12 file, now how do I concatenate these 12 files? I've tried cat <files> and paste <files>, but after using diff, whole file was different from the original.

How do I concatenate these 12 files so that cmp/diff will show no differences? Any special arguments for paste/cat to use?

CodePudding user response:

Is round robin splitting an absolute requirement? If not you might just split into sections:

$split --number=12 file

This creates 12 files:

$ ls x*
xaa xab xac xad xae xaf xag xah xai xaj xak xal

Now you can concat without any difference:

$cat x* > file.new

$diff file file.new

But if there is no way around the round robin requirement I would create a bash script - not pretty. Just providing a pseudocode

Something like:

Create working directory
Copy all x* files into working directory
Change to working directory
Touch new concatenated file

While all x* files are not empty
  Iterate over files in alpha order
     Remove the first line in file
     Append the line to the new concatenated file
  •  Tags:  
  • Related