# Using sed for In-Place Editing of Genomic Files

By the end of this guide you will be able to edit FASTA and BED files directly on disk with `sed` (stream editor), rename chromosome labels and sequence headers, strip descriptions from FASTA headers, filter BED rows by strand, fix line-ending problems, verify every edit with `diff` and `awk`, and recover instantly from a bad edit using backup suffixes. You need a Linux or macOS shell, GNU sed installed (the default on Linux), and a couple of small practice files. All commands run under a standard bash shell, which is the foundation of Linux for bioinformatics work.

Everything here works on plain text. If your files are compressed, if they hold millions of reads, or if the edit requires arithmetic on coordinates, later sections point out the better tool for the job.

## Why sed for In-Place Editing of FASTA and BED Files

![Decision path asking whether the edit is a uniform text substitution, then routing to sed, awk, or a dedicated genomics tool.](/article-images/illustrations/sed-in-place-edit-fasta-bed-sed-or-awk.webp)

FASTA and BED files are plain text with a simple line structure. That makes them ideal targets for sed, a stream editor designed to apply text substitutions line by line as a file flows past. A single sed command can rename `chrMT` to `chrM` across a two-gigabyte BED file without opening an editor, and the in-place option writes the result back to the same filename.

The phrase "in-place editing" deserves a precise definition. `sed -i` does not truly modify bytes inside the existing file. GNU sed writes a temporary file, streams your edited content into it, and then renames that temporary file over the original. This behavior has two practical consequences you will see again later. First, it breaks hard links and symbolic links, because the file the links point to is replaced by a new one. Second, if the disk fills up mid-write, you can lose data, which is why backups matter even when the tool sounds safe.

Most Linux command bioinformatics tutorials use sed for quick, mechanical text edits. sed is the right tool when the change is a pattern substitution applied uniformly across lines. It is the wrong tool when the edit depends on relationships between lines, such as merging wrapped sequence lines in FASTA or shifting coordinates in BED. Those cases need awk, python, or dedicated genomics tools, and this guide says so explicitly at each decision point.

## FASTA and BED Format Essentials

You cannot edit a format safely without knowing its rules, so here is a short refresher on both.

### FASTA structure

FASTA stores nucleotide or protein sequences as plain text. Each record begins with a header line starting with the greater-than character, followed by the sequence identifier and an optional description. Sequence lines follow, usually wrapped at a fixed width such as 60 or 80 characters. A minimal example:

```
>chr1 human chromosome 1 fragment
ATGGCCGATTACAGTT
GGCAATCCGTA
>chr2 human chromosome 2 fragment
TTGACCAAGGTT
CCATGG
```

Two facts drive every sed decision with FASTA. First, header lines start with `>`, which gives you a clean anchor for addressing. Second, sequence data is split across multiple lines, so any edit that must span a whole record (for example, inserting text after a specific sequence) exceeds what a line-oriented tool handles cleanly.

### BED structure

BED (Browser Extensible Data) describes genomic intervals. The [UCSC Genome Browser](/knowledge/bioinformatics/ucsc-genome-browser-beginner-tutorial-and-tips) documents BED as having at least three required fields, `chrom`, `chromStart`, and `chromEnd`, with optional fields such as `name`, `score`, and `strand` after them. The coordinate convention matters enormously for sed work that touches numbers. UCSC defines `chromStart` as zero-based and the interval as half-open, meaning the start position is included in the interval and the end position is not.

A six-column BED example:

```
chr1	10000	11000	geneA	0	+
chr1	25000	26500	geneB	0	-
chrM	300	800	geneC	0	+
```

Fields are tab-separated by convention. Verify this in your own files, because a BED file that uses spaces instead of tabs will break downstream tools even though it looks fine on screen. Throughout this guide, commands use `\t` to match a literal tab, which GNU sed interprets directly.

## In-Place Editing Mechanics: The -i Option

Without `-i`, sed prints edited text to standard output and leaves the file untouched:

```
sed 's/chrMT/chrM/' loci.bed
```

With `-i`, GNU sed replaces the file on disk:

```
sed -i 's/chrMT/chrM/' loci.bed
```

The safest habit, and the one this walkthrough uses for every command, is to give `-i` a backup suffix:

```
sed -i.bak 's/chrMT/chrM/' loci.bed
```

This command edits `loci.bed` in place and saves the unmodified original as `loci.bed.bak`. GNU sed documents the backup-suffix form of `-i` exactly this way. The cost is disk space (you keep two copies of every file), and the benefit is a one-keystroke recovery from any mistake.

### GNU versus BSD sed on macOS

![Comparison table of GNU and BSD sed in-place flags, backup behavior, and the macOS fix using an empty suffix or gsed.](/article-images/illustrations/sed-in-place-edit-fasta-bed-gnu-vs-bsd.webp)

If you work in Linux for bioinformatics on a shared cluster, you have GNU sed and everything above works as written. On macOS you get BSD sed, where `-i` behaves differently. BSD sed treats the text immediately after `-i` as the backup suffix, so `sed -i 's/a/b/' file` fails with an error because sed tries to use the script itself as the suffix. On macOS you must supply an explicit suffix argument, often an empty one:

```
sed -i '' 's/chrMT/chrM/' loci.bed
```

That single quoting difference is the most common reason a sed command copied from a tutorial fails on a Mac. Either install GNU sed through Homebrew (`gsed`) or adjust the flag for the platform you are on.

## Step-by-Step Workflow with Real Commands

The following sed in-place edit FASTA BED step-by-step walkthrough uses small sample files so you can see exact input and output. Every command follows the same discipline: test on screen first, then apply with a backup suffix.

### Step 1: Set Up a Test Directory and Sample Files

Create a scratch directory and two small files. Do this on the command line:

```
mkdir sed_practice
cd sed_practice
```

```
printf 'chr1\t10000\t11000\tgeneA\t0\t+\nchr1\t25000\t26500\tgeneB\t0\t-\nchrMT\t300\t800\tgeneC\t0\t+\nchr2\t50000\t51000\tgeneD\t0\t-\n' > loci.bed
```

```
printf '>chr1 human chromosome 1 fragment\natggccgattacagtt\nggcaatccgta\n>chr2 human chromosome 2 fragment\nttgaccaaggtt\nccatgg\n' > sample.fa
```

Confirm the contents before touching anything:

```
cat loci.bed
```

Expected output:

```
chr1	10000	11000	geneA	0	+
chr1	25000	26500	geneB	0	-
chrMT	300	800	geneC	0	+
chr2	50000	51000	geneD	0	-
```

```
cat sample.fa
```

Expected output:

```
>chr1 human chromosome 1 fragment
atggccgattacagtt
ggcaatccgta
>chr2 human chromosome 2 fragment
ttgaccaaggtt
ccatgg
```

Note that the sequences are lowercase. Most aligners and variant callers accept either case, but some downstream tools and database comparisons expect uppercase, which gives us a natural editing task later.

### Step 2: Test Every Command Without -i First

The single best tip for using sed for in-place editing of genomic files is to separate testing from editing. Run the substitution with no `-i` flag and look at what prints to the terminal:

```
sed 's/chrMT/chrM/' loci.bed
```

Expected output:

```
chr1	10000	11000	geneA	0	+
chr1	25000	26500	geneB	0	-
chrM	300	800	geneC	0	+
chr2	50000	51000	geneD	0	-
```

The original file is untouched. Only when the terminal output looks exactly right do you rerun the command with `-i.bak`. This two-step habit costs a few seconds and has saved more genomes-research projects than any other practice in this guide.

### Step 3: Rename Chromosome Labels in a BED File

Renaming `chrMT` to `chrM` in place, keeping a backup:

```
sed -i.bak 's/chrMT/chrM/' loci.bed
```

Verify:

```
grep 'chrM' loci.bed
```

Expected output:

```
chrM	300	800	geneC	0	+
```

The same substitution works in FASTA headers, which is handy when a reference genome uses a different chromosome naming scheme than your annotation file. Suppose you need to strip the `chr` prefix entirely, as required by tools that expect bare numbers:

```
sed -i.bak 's/^chr//' loci.bed
cat loci.bed
```

Expected output:

```
1	10000	11000	geneA	0	+
1	25000	26500	geneB	0	-
M	300	800	geneC	0	+
2	50000	51000	geneD	0	-
```

The `^` anchor restricts the substitution to the start of the line, which is always the chromosome field in BED. That anchor is what makes this command safe. Without it, `s/chr//` would silently corrupt any gene name or description containing the letters chr.

To run the reverse and add `chr` to numeric chromosome names only, use an extended regular expression with `-E`:

```
sed -E -i.bak 's/^([0-9MXY]+)\t/chr\1\t/' loci.bed
```

Here `-E` enables extended regular expressions so that `+` and `\1` behave predictably, the capture group `([0-9MXY]+)` matches chromosome identifiers made of digits or the letters M, X, or Y, and `\1` writes the captured text back with the prefix added. This tip illustrates a general principle: the more specific your pattern, the safer the in-place edit.

### Step 4: Edit Specific Lines and Columns in a BED File

sed addresses let you apply a substitution only to matching lines. To delete every minus-strand row from a BED file in place:

```
sed -i.bak '/\t-$/d' loci.bed
cat loci.bed
```

Expected output:

```
chr1	10000	11000	geneA	0	+
chrM	300	800	geneC	0	+
```

The address `/\t-$/` matches lines whose last character, before the newline, is a minus sign preceded by a tab. The `d` command deletes matching lines. Because strand is conventionally the last BED column, the end-of-line anchor `$` does the heavy lifting. If your BED files carry more columns after strand, anchor differently or switch to awk, which sees fields rather than text.

To append a seventh column with a fixed value to every line:

```
sed -i.bak 's/$/\t4.2/' loci.bed
```

To edit only rows on a given chromosome, address by pattern and apply the substitution to those lines:

```
sed -i.bak '/^chr1\t/s/geneA/geneA_isoform1/' loci.bed
```

Read this as: on lines starting with `chr1` followed by a tab, replace `geneA` with `geneA_isoform1`. The substitution is scoped to the address, so identical text elsewhere in the file stays untouched.

One caution about numbers. sed does pattern substitution, not arithmetic. If you need to shift coordinates by a fixed offset, convert between BED and GFF conventions, or merge overlapping intervals, use awk or bedtools. Editing coordinate columns with sed invites off-by-one errors precisely because the zero-based, half-open BED convention documented by UCSC is easy to forget mid-substitution.

### Step 5: Edit FASTA Headers

Strip the description from every header, keeping only the sequence identifier:

```
sed -i.bak '/^>/s/ .*//' sample.fa
cat sample.fa
```

Expected output:

```
>chr1
atggccgattacagtt
ggcaatccgta
>chr2
ttgaccaaggtt
ccatgg
```

The address `/^>/` restricts the substitution to header lines. On those lines only, `s/ .*//` deletes a space and everything after it. Sequence lines never match the address, so they pass through unchanged. This one command is probably the most repeated sed one-liner in bioinformatics, used before building BLAST databases or renaming transcripts to match a mapping table.

To rename a specific identifier:

```
sed -i.bak 's/>chr1/>NC_000001/' sample.fa
```

Only the first line matches, because `>chr1` at the start of a line occurs once.

### Step 6: Change Sequence Case and Clean Up FASTA Bodies

To uppercase every sequence character without touching the headers, use the sed `y` command, which transliterates characters, combined with a header exclusion:

```
sed -i.bak '/^>/!y/acgtn/ACGTN/' sample.fa
cat sample.fa
```

Expected output:

```
>chr1
ATGGCCGATTACAGTT
GGCAATCCGTA
>chr2
TTGACCAAGGTT
CCATGG
```

The address `/^>/!` means "on lines that do not start with a greater-than character." The `y` command then maps each lowercase letter to its uppercase partner, one character at a time. The two strings in `y///` must be the same length, and unlike `s`, the `y` command takes no flags.

Remove stray blank lines that break some FASTA parsers:

```
sed -i.bak '/^$/d' sample.fa
```

### Step 7: Fix Line Endings and Hidden Whitespace

Files created on Windows or in some spreadsheet tools carry carriage return characters that end lines with CRLF instead of LF. Downstream tools then see chromosome names like `chr1\r` and fail with confusing errors. Strip the carriage returns in place:

```
sed -i.bak 's/\r$//' sample.fa
```

The pattern `\r$` targets a carriage return at the very end of each line. GNU sed understands `\r` as a carriage return escape directly. To also trim trailing whitespace of any kind from every line, use an extended expression:

```
sed -E -i.bak 's/[[:space:]]+$//' loci.bed
```

For BED files specifically, check that your separator is a real tab. You can reveal tabs with this quick test:

```
grep -P '\t' loci.bed | head -n 2
```

If a file was built by copying from a word processor, replace runs of spaces between fields with a single tab:

```
sed -E -i.bak 's/ +/\t/g' loci.bed
```

The `g` flag matters here, as the next section explains.

### Step 8: Combine Edits and Process Multiple Files

GNU sed accepts multiple `-e` expressions, applied in order in a single pass:

```
sed -i.bak -e 's/\r$//' -e '/^$/d' -e '/^>/!y/acgtn/ACGTN/' sample.fa
```

This performs all three edits in one read of the file, which matters when the file is tens of gigabytes. Note the use of separate `-e` flags rather than chained commands inside one expression. It keeps each edit readable and independently testable.

The shell, not sed, expands wildcards, so the same command applied to every BED file in a directory looks like this:

```
sed -i.bak 's/chrMT/chrM/' *.bed
```

Each matching file gets its own `.bak` sibling. To clean up after verification:

```
rm *.bak
```

Only run that removal after you have checked the results, because it is the moment the backups stop protecting you.

## Key sed Options and Syntax Reference

The following table gathers the options and syntax elements used throughout this workflow.

| Option or syntax | What it does | Example |
|--|--|--|
| `-i.bak` | Edit in place, keep original as `.bak` | `sed -i.bak 's/chrMT/chrM/' loci.bed` |
| `-i` | Edit in place with no backup (GNU) | `sed -i 's/chrMT/chrM/' loci.bed` |
| `-i ''` | BSD/macOS in-place edit with no backup | `sed -i '' 's/chrMT/chrM/' loci.bed` |
| `-E` | Extended regular expressions | `sed -E 's/^(+)\t/chr\1\t/'` |
| `-n` | Suppress default printing, print only what you ask | `sed -n '/\t+$/p' loci.bed` |
| `-e` | Add another expression, applied in sequence | `sed -e 's/\r$//' -e '/^$/d' file` |
| `s/old/new/` | Substitute first match per line | `s/chrMT/chrM/` |
| `s/old/new/g` | Substitute every match per line | `s/ +/\t/g` |
| `s/old/new/I` | Case-insensitive match | `s/^chr/chr/I` |
| `/^>/` | Address header lines in FASTA | `/^>/s/ .*//` |
| `/^>/!` | Address every line except headers | `/^>/!y/acgtn/ACGTN/` |
| `$` as address | Last line of the file | `'$s/$/\tend/'` |
| `\t` | Literal tab (GNU sed) | `/\t-$/d` |
| `y/a/A/` | Character-by-character transliteration, no flags | `y/acgtn/ACGTN/` |
| `\1` | First capture group in replacement | `s/^(+)\t/chr\1\t/` |

## How Each Piece Works

Understanding four sed concepts turns copied commands into commands you can adapt.

**Addresses.** An address before a command decides which lines the command touches. An address can be a pattern between slashes (`/^>/`), a line number (`3s/x/y/`), the dollar sign for the last line, or a range (`2,5s/x/y/`). Unaddressed commands apply to every line. In genomic files, addresses are your safety rails: anchor to `^>` in FASTA and to line starts or tab-adjacent positions in BED.

**Substitution flags.** Without flags, `s` replaces only the first match on each line. The `g` (global) flag replaces every match on the line. The `I` flag makes matching case-insensitive. Forgetting `g` when you need it is one of the most common sed mistakes in general, and it bites hard when a repeated pattern appears several times per line, such as whitespace runs in a malformed BED file.

**Delimiters.** The `s` command accepts any character as its delimiter. This matters when your pattern or replacement contains slashes, as paths do. These two commands are equivalent in effect:

```
sed 's/old\/path/new\/path/' notes.txt
```

```
sed 's|old/path|new/path|' notes.txt
```

Using `|` avoids escaping every slash.

**Line orientation.** sed reads one line at a time and has no memory of the previous line unless you explicitly hold text in its pattern space with advanced commands. This is why wrapped FASTA sequences resist straightforward sed edits. The `y` command, the `d` command, and `s` all act within a single line. Anything requiring record-level logic across lines belongs in awk or a scripting language.

One more tip that matters at real-world scale: set the locale to plain bytes for large files. Prefix a command like this:

```
LC_ALL=C sed -i.bak 's/chrMT/chrM/' huge.loci.bed
```

Under a plain C locale, sed matches bytes rather than interpreting multibyte character classes, which both speeds up huge-file processing and gives predictable behavior on ASCII genomic text.

## Checking That Your Edits Are Correct

Never trust a silent success. In-place editing leaves no terminal output to review, so verification is a separate, deliberate step.

**Compare against the backup.** The GNU diff utilities show exactly what changed between the backup and the edited file:

```
diff -u loci.bed.bak loci.bed
```

Expected output for the chromosome rename:

```
--- loci.bed.bak
+++ loci.bed
@@ -1,4 +1,4 @@
-chrMT	300	800	geneC	0	+
+chrM	300	800	geneC	0	+
 chr1	10000	11000	geneA	0	+
 chr1	25000	26500	geneB	0	-
 chr2	50000	51000	geneD	0	-
```

Reading the diff before deleting the `.bak` files is the final gate in the workflow.

**Count records.** For FASTA, the number of header lines equals the number of records, and edits should not change that count:

```
grep -c '^>' sample.fa
```

Expected output:

```
2
```

**Check BED column counts.** Every BED line should have the same number of tab-separated fields. Report the line numbers of any line that does not have six:

```
awk -F '\t' 'NF != 6 {print NR}' loci.bed
```

No output means every line has six fields. This check catches stray spaces, truncated lines, and carriage-return damage in one pass.

**Check sequence alphabet.** Confirm that sequence lines contain only expected characters after any case or cleanup edit:

```
grep -v '^>' sample.fa | grep -v '^[ACGTN]*$'
```

No output means every non-header line consists solely of A, C, G, T, and N. Extend the bracket expression with ambiguity codes if your data uses them.

**Verify sorted coordinates where required.** Many downstream tools require BED input sorted by chromosome and start position. Ask `sort` to check rather than re-sorting blindly:

```
sort -k1,1 -k2,2n -c loci.bed
```

No output means the file is already in the required order. If it reports a disordering, re-sort with the same key arguments and write to a new file before proceeding.

## Common Mistakes and Limitations

This section collects the sed in-place edit FASTA BED common mistakes that cause silent data corruption or lost files, along with the limits of the tool.

**Editing without a backup.** `sed -i` with no suffix destroys the original with no recovery path short of a backup system. GNU sed overwrites the target by renaming a freshly written temporary file over it. Always use `-i.bak` until you have verified the result, and delete backups only after a diff review.

**Forgetting the `g` flag.** `s/ +/\t/` converts only the first whitespace run per line, leaving a half-converted BED file whose columns silently realign. When every occurrence must change, write `g`. When only the first must change, omitting `g` is the correct choice, so decide consciously.

**Assuming macOS sed behaves like GNU sed.** The BSD `-i` flag consumes the next argument as a suffix. A GNU-style command on a Mac either fails with `sed: 1: "loci.bed": extra characters at the end of l command` or, worse, creates a backup file named after your script text. Fix by quoting an explicit suffix (`sed -i ''`) or by installing GNU sed as `gsed` and using it consistently.

**Losing links and permissions expectations.** Because `-i` replaces the file rather than rewriting it, hard links to the original inode no longer see your edit, and a symlink gets severed rather than followed. On projects where sequence data is symlinked into per-sample directories, edit the canonical file, not the link.

**Unescaped special characters.** In sed patterns, a period matches any single character, so `s/chr1./chr1_/` can match `chr10`, `chr1X`, or even a tab after chr1. Escape metacharacters you mean literally (`chr1\.`) or anchor them. The same rule applies to asterisks and brackets. When your search text includes a delimiter, switch delimiters rather than stacking backslashes.

**Treating coordinates as text.** The BED convention of zero-based, half-open intervals is documented by UCSC and enforced by every serious downstream tool. sed can replace a number with another number, but it cannot safely increment coordinates, and a "quick" sed coordinate edit is a classic source of one-base shifts that surface weeks later as discrepant variant calls. Do arithmetic in awk, python, or bedtools.

**Assuming sed understands records, not lines.** Wrapping in FASTA means a sequence spans many lines. Commands like `y/acgtn/ACGTN/` work per line and are therefore safe, but anything requiring knowledge of which lines belong to which record, such as extracting a sequence by identifier or joining wrapped lines, exceeds sed's natural model. Reach for awk or a purpose-built FASTA toolkit at that point.

**Feeding sed compressed files.** sed is a text tool. Running it against a `.gz` file produces binary noise and can corrupt the archive if combined with in-place output. Decompress first (`gunzip`), edit the plain text, then recompress, or stream with `zcat file.gz | sed ... > out` for read-only transforms.

**Large-file performance mistakes.** Default locales with complex character rules slow sed down dramatically on multigigabyte files. Setting `LC_ALL=C` for the sed invocation is standard Linux commands bioinformatics practice for genomic text. Also prefer single-pass multi-expression commands (`-e`) over piping the file through several sed invocations, which decompresses and rewrites the data repeatedly.

**Editing the only copy.** A mistake that transcends sed: never run in-place edits on the sole copy of primary data. In-place editing belongs on derived working files, with the original read-only source retained elsewhere. Validate the tool on a small extracted region of your real file before pointing it at the full dataset.

A final boundary on scope. sed handles mechanical text substitution well and nothing more. Production pipelines that must be reproducible, version-controlled, and tested belong in workflow managers with pinned tool versions, not in hand-edited one-off commands. Any analysis intended for publication or clinical decision-making needs that level of rigor, and quick edits like the ones in this guide sit upstream of it, not in place of it.

## Frequently Asked Questions

### How do I edit a FASTA file in place with sed without losing the original?

Use the backup-suffix form of the in-place option, for example `sed -i.bak 's/>old/>new/' sequences.fa`, which writes the edit and preserves the untouched original as `sequences.fa.bak`. Check the result with `diff -u sequences.fa.bak sequences.fa` before removing the backup.

### Why does sed -i work on Linux but fail on macOS?

GNU sed on Linux treats `-i` as a flag with an optional attached suffix, while macOS BSD sed treats the text after `-i` as a required suffix argument. On macOS, write `sed -i '' 's/old/new/' file` for no backup, or install GNU sed as `gsed` for identical behavior across platforms.

### Can sed edit a gzip-compressed FASTA file in place?

No. sed operates on plain text and cannot read or rewrite gzip archives. Decompress the file with `gunzip`, edit the plain text with sed, then recompress, or stream through a pipe with `zcat file.gz | sed 's/.../.../' > out.txt` for non-destructive transformations.

### How do I remove the chr prefix from every chromosome name in a BED file?

Run `sed -i.bak 's/^chr//' loci.bed`. The `^` anchor restricts removal to the start of each line, which in BED is always the chromosome field, so occurrences of chr inside gene names remain untouched.

### How do I rename a sequence identifier in a FASTA header without touching the sequence?

Anchor the substitution to header lines with an address, for example `sed -i.bak 's/>chr1/>NC_000001/' sample.fa` to rename one identifier, or `sed -i.bak '/^>/s/ .*//' sample.fa` to strip descriptions from every header while leaving sequence lines alone.

### Does sed process one line at a time, and why does that matter for FASTA files?

Yes, sed reads and edits one line at a time and does not naturally track which lines belong to the same record. That makes per-line edits such as uppercasing safe, but record-level tasks such as joining wrapped sequence lines or extracting a sequence by identifier need awk or a dedicated FASTA tool.

### How can I test a sed command before applying it in place?

Run the same command without `-i` and read the output it prints to the terminal, because that output is exactly what would be written to the file. You can also redirect to a scratch file with `sed 's/.../.../' in.bed > test.bed` and compare with `diff -u in.bed test.bed`.

### When should I use awk or a dedicated tool instead of sed?

Use awk when the edit depends on field positions, numeric conditions, or arithmetic, such as filtering BED rows by interval length or shifting coordinates. Use dedicated tools for coordinate-aware operations such as interval intersection, and reserve sed for uniform pattern substitutions that apply line by line.

<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "FAQPage",
  "mainEntity": [
    {
      "@type": "Question",
      "name": "How do I edit a FASTA file in place with sed without losing the original?",
      "acceptedAnswer": {
        "@type": "Answer",
        "text": "Use the backup-suffix form of the in-place option, for example sed -i.bak 's/>old/>new/' sequences.fa, which writes the edit and preserves the untouched original as sequences.fa.bak. Check the result with diff before removing the backup."
      }
    },
    {
      "@type": "Question",
      "name": "Why does sed -i work on Linux but fail on macOS?",
      "acceptedAnswer": {
        "@type": "Answer",
        "text": "GNU sed on Linux treats -i as a flag with an optional attached suffix, while macOS BSD sed treats the text after -i as a required suffix argument. On macOS, write sed -i '' for no backup, or install GNU sed as gsed for identical behavior across platforms."
      }
    },
    {
      "@type": "Question",
      "name": "Can sed edit a gzip-compressed FASTA file in place?",
      "acceptedAnswer": {
        "@type": "Answer",
        "text": "No. sed operates on plain text and cannot read or rewrite gzip archives. Decompress the file with gunzip, edit the plain text with sed, then recompress, or stream through a pipe with zcat and sed for non-destructive transformations."
      }
    },
    {
      "@type": "Question",
      "name": "How do I remove the chr prefix from every chromosome name in a BED file?",
      "acceptedAnswer": {
        "@type": "Answer",
        "text": "Run sed -i.bak 's/^chr//' loci.bed. The ^ anchor restricts removal to the start of each line, which in BED is always the chromosome field, so occurrences of chr inside gene names remain untouched."
      }
    },
    {
      "@type": "Question",
      "name": "How do I rename a sequence identifier in a FASTA header without touching the sequence?",
      "acceptedAnswer": {
        "@type": "Answer",
        "text": "Anchor the substitution to header lines with an address, for example sed -i.bak 's/>chr1/>NC_000001/' sample.fa to rename one identifier, or sed -i.bak '/^>/s/ .*//' sample.fa to strip descriptions from every header while leaving sequence lines alone."
      }
    },
    {
      "@type": "Question",
      "name": "Does sed process one line at a time, and why does that matter for FASTA files?",
      "acceptedAnswer": {
        "@type": "Answer",
        "text": "Yes, sed reads and edits one line at a time and does not naturally track which lines belong to the same record. That makes per-line edits such as uppercasing safe, but record-level tasks such as joining wrapped sequence lines or extracting a sequence by identifier need awk or a dedicated FASTA tool."
      }
    },
    {
      "@type": "Question",
      "name": "How can I test a sed command before applying it in place?",
      "acceptedAnswer": {
        "@type": "Answer",
        "text": "Run the same command without -i and read the output it prints to the terminal, because that output is exactly what would be written to the file. You can also redirect to a scratch file and compare it with the original using diff."
      }
    },
    {
      "@type": "Question",
      "name": "When should I use awk or a dedicated tool instead of sed?",
      "acceptedAnswer": {
        "@type": "Answer",
        "text": "Use awk when the edit depends on field positions, numeric conditions, or arithmetic, such as filtering BED rows by interval length or shifting coordinates. Use dedicated tools for coordinate-aware operations such as interval intersection, and reserve sed for uniform pattern substitutions that apply line by line."
      }
    }
  ]
}
</script>