<ol>
<li>Convert each of your Excel spreadsheets to tab-delimited text files (<a href="https://www.howtogeek.com/79991/convert-an-excel-spreadsheet-to-a-tab-delimited-text-file/" rel="nofollow noreferrer">how-to</a>).</li>
<li><a href="https://cygwin.com/install.html" rel="nofollow noreferrer">Install and run Cygwin</a>, if using Windows. If you are using OS X, open up the Terminal app.</li>
<li><a href="https://bedops.readthedocs.io/en/latest/content/installation.html" rel="nofollow noreferrer">Install the BEDOPS toolkit</a> to get sorting and set operation tools (if you're not averse to using them).</li>
<li><p>Strip out the Microsoft line endings, strip the header, and sort each of the <em>N</em> files:</p>

<pre><code>$ tr -d '\r' &lt; fileN.tsv | tail -n+2 - | sort-bed - &gt; fileN.bed
</code></pre></li>
<li><p>Merge the files:</p>

<pre><code>$ bedops --merge file1.bed file2.bed ... fileN.bed &gt; answer.bed
</code></pre></li>
</ol>

<p>The file <code>answer.bed</code> will contain all merged regions from files <code>file1.bed</code> through <code>fileN.bed</code>. Merging will be done on intervals for each unique chromosome name.</p>

CC BY-SA 4.0
2020-04-04T20:47:47.863Z
Thanks that worked
Mariam
CC BY-SA 4.0
2020-04-03T07:33:53.447Z
2020-04-03T07:33:53.447Z
Alex Reynolds
<p>In my opinion, the easiest way to merge bed files is to use <code>bedtools merge</code>.</p>

CC BY-SA 4.0
2020-04-04T15:17:45.307Z
2020-04-04T15:17:45.307Z
Phoenix Mu
<p>To merge multiple BED files into one, you need to do the following:</p>
<ul>
<li>concatenate them all (<code>bedtools merge</code> only accepts one input BED)</li>
<li>sort the concatenated BED</li>
<li>merge the sorted BED with <a href="https://bedtools.readthedocs.io/en/latest/content/tools/merge.html" rel="nofollow noreferrer"><code>bedtools merge</code></a></li>
</ul>
<p>Here is a simple pipeline to do this</p>
<pre><code>cat a.bed b.bed | sort -k 1,1 -k2,2n | bedtools merge &gt; merged.bed
</code></pre>

CC BY-SA 4.0
2024-07-05T02:27:24.277Z
2024-07-05T02:27:24.277Z
Michael Hall
<p>I am trying to merge two bed files (more in future) to one. my bed files are something like : 
<a href="https://i.sstatic.net/dcAAi.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/dcAAi.png" alt="result of macs pea calling"></a>.</p>

<p>I need to merge them in a way to have the shared chromosome location. </p>

<p>Is there a way to do that ?</p>

<phylogenetics><samtools><sequence-analysis><bedtools><macs2>
merging two/or more bed file into one bed file
# merging two/or more bed file into one bed file

(Asked by: username_0 on 2020-04-03)

I am trying to merge two bed files (more in future) to one. my bed files are something like :
[![result of macs pea calling](https://i.sstatic.net/dcAAi.png)](https://i.sstatic.net/dcAAi.png).

I need to merge them in a way to have the shared chromosome location.

Is there a way to do that ?

**Tags:** phylogenetics, samtools, sequence-analysis, bedtools, macs2

---

## 3 answers

### Answer by username_1 on 2020-04-03. Score: 3

Convert each of your Excel spreadsheets to tab-delimited text files (how-to).
Install and run Cygwin, if using Windows. If you are using OS X, open up the Terminal app.
Install the BEDOPS toolkit to get sorting and set operation tools (if you're not averse to using them).
Strip out the Microsoft line endings, strip the header, and sort each of the N files:
$ tr -d '\r' < fileN.tsv | tail -n+2 - | sort-bed - > fileN.bed

Merge the files:
$ bedops --merge file1.bed file2.bed ... fileN.bed > answer.bed


The file answer.bed will contain all merged regions from files file1.bed through fileN.bed. Merging will be done on intervals for each unique chromosome name.

> **Comments:**
> **username_0**: Thanks that worked  

---

### Answer by username_2 on 2020-04-04. Score: 2

In my opinion, the easiest way to merge bed files is to use `bedtools merge`.


---

### Answer by username_3 on 2024-07-05. Score: 0

To merge multiple BED files into one, you need to do the following:

concatenate them all (bedtools merge only accepts one input BED)
sort the concatenated BED
merge the sorted BED with bedtools merge

Here is a simple pipeline to do this
cat a.bed b.bed | sort -k 1,1 -k2,2n | bedtools merge > merged.bed


---

