Bulk Replace
Regex

How to Add Text to the Start or End of Every Line in Many Files

Add a prefix or suffix to every line in one file or hundreds, using regex ^ and $ anchors. Skip blank lines, wrap lines in quotes and use PowerShell.

Updated 5 minute read The Bulk Replace team

To add text to the beginning of every line, use a regex replace with multiline mode on: find ^ and replace it with the text you want, such as // or > . To skip blank lines, find ^(?=[^\r\n]) instead. To add text to the end of every non-empty line, find ([^\r\n]+) and replace with $1; (or whatever suffix you need). These patterns work in one file or across hundreds, and they handle both Windows and Unix line endings.

Below are the patterns explained, a set of ready-made variations (quotes, commas, comments, indentation), and the same jobs done in PowerShell.

Adding a prefix: the ^ anchor

^ matches a position, not a character: the start of a line when multiline mode is on. Replacing that empty position with text inserts the text without deleting anything. Editors such as Notepad++ and VS Code treat ^ as the start of each line by default. In PowerShell and other .NET tools, add (?m) to the start of the pattern.

The plain ^ has one side effect: it matches at the start of empty lines too, and in .NET it also matches after the very last line break of a file, which adds a stray prefix on an extra line at the end. The lookahead version avoids both:

FindMatches the start ofUse when
^Every line, including blank onesYou really want every line, such as numbering a block
^(?=[^\r\n])Every line with at least one characterMost jobs: commenting out, quoting, prefixing
^(?=\S)Lines that start with a visible characterSkip lines that are already indented
^(?=import )Only lines starting with import Prefix lines that match a condition

Note the use of [^\r\n] rather than .. Some engines treat a lone \r as a line end, so on Windows files ^ can match between \r and \n. The class [^\r\n] refuses that position, which keeps the pattern safe in every tool.

Adding a suffix: capture the line

To add text to the end of a line, the most reliable approach is to capture the line content and put it back with the suffix:

  • Find: ([^\r\n]+)
  • Replace: $1;

[^\r\n]+ grabs a whole line's content up to, but not including, the line ending. The replacement writes it back with a semicolon after it. Empty lines are skipped because + needs at least one character. This pattern does not need multiline mode at all.

Why not use $? Finding $ and replacing with ; works in editors, but in .NET $ matches after the \r on Windows files, so the semicolon ends up between \r and \n. The file then looks fine in some editors and wrong in others. Capturing with [^\r\n]+ avoids that. The same \r issue is explained in removing trailing whitespace from files.

If you use a dot, as in (.+), the hidden \r of a Windows line ending can be captured as part of the line in some engines, which puts your suffix after it. Stick to [^\r\n].

Ready-made patterns

TaskFindReplaceResult
Comment out lines (C, JS, C#)^(?=[^\r\n])// // foo();
Comment out lines (Python, PowerShell, INI)^(?=[^\r\n])# # setting=1
Quote for email or Markdown^> > text
Indent by four spaces^(?=[^\r\n])four spaces text
Wrap in double quotes([^\r\n]+)"$1""text"
Make an SQL IN list([^\r\n]+)'$1','A123',
Add a trailing comma([^\r\n]+)$1,value,
Wrap in list items([^\r\n]+)<li>$1</li><li>text</li>
Add a suffix only if missing([^\r\n]*[^;\r\n])(?=\r?$)$1;Lines already ending in ; are skipped

For the last row, multiline mode must be on. The pattern only matches a line whose last character is not a semicolon, so running it twice does not add a second one.

To remove a prefix later, reverse it with an anchored pattern, for example find ^// and replace with nothing. The anchor makes sure only the prefix at the start of the line is removed. Our guide to capture groups explains how $1 works if you want to build your own variations.

In PowerShell

PowerShell can do this line by line without any regex, which is easy to read:

  • Prefix every line: (Get-Content .\list.txt) | ForEach-Object { "> $_" } | Set-Content .\list.txt
  • Suffix non-empty lines: (Get-Content .\list.txt) | ForEach-Object { if ($_ -ne '') { "$_;" } else { $_ } } | Set-Content .\list.txt
  • Wrap in quotes: (Get-Content .\ids.txt) | ForEach-Object { "'$_'," } | Set-Content .\ids-sql.txt

Here $_ is the current line, and double quotes let PowerShell insert it. Get-Content strips the line endings, and Set-Content writes each line back with \r\n, so line endings are always Windows-style afterwards. In Windows PowerShell 5.1, add -Encoding to Set-Content if your files contain non-English characters.

For many files, wrap the regex version in a loop:

Get-ChildItem -File -Filter *.sql | ForEach-Object { $t = Get-Content $_.FullName -Raw; Set-Content $_.FullName ($t -replace '(?m)^(?=[^\r\n])', '-- ') -NoNewline }

That comments out every non-empty line in every .sql file in the current folder.

Only some lines: combine with a condition

Most real jobs are not "every line" but "every line that matches something". Put the condition in a lookahead after ^:

  • Comment out debug logging: find ^(?=[ \t]*console\.log), replace // .
  • Disable settings that mention an old server: find ^(?=[^\r\n]*oldserver), replace # .
  • Mark TODO lines: find ([^\r\n]*TODO[^\r\n]*), replace $1 [check].

The lookahead checks the line without consuming it, so the prefix is still inserted at the very start. See lookahead and lookbehind examples for more conditions you can use.

Across many files at once

Adding a prefix or suffix across a folder is low risk in one sense, since nothing is deleted, but it is easy to apply to the wrong files or run twice. A few habits help:

  1. Filter by extension so the change only reaches the file types it belongs in.
  2. Use the "only if missing" form where you can, so a second run does nothing.
  3. Check the match count. For a prefix on every non-empty line, it should roughly equal the number of non-empty lines.
  4. Open a Windows-format file afterwards and check that the suffix sits at the end of the visible text, not on a line of its own.

Bulk Replace runs these as regex rules over a whole folder tree, with a before/after diff of each file, so you can see the prefix in place before anything is saved. Because the capture pattern stops before the line break, the suffix lands in the right place in both Windows and Unix files. Bulk Replace also keeps each file's encoding, and every run can be undone in one click.

Quick reference

  • Prefix non-empty lines: ^(?=[^\r\n]) → prefix
  • Suffix non-empty lines: ([^\r\n]+) → $1suffix
  • Wrap lines: ([^\r\n]+) → "$1"
  • Remove a prefix: ^prefix → nothing (escape any special characters)

Frequently asked questions

What regex adds text to the start of every line?

With multiline mode on, find ^ and replace with your prefix. To skip blank lines, find ^(?=[^\r\n]) instead, which also works with Windows line endings.

How do I add text to the end of every line?

Find ([^\r\n]+) and replace with $1; to add a semicolon after every non-empty line, which works with both Windows and Unix line endings.

How do I wrap each line in quotes?

Find ([^\r\n]+) and replace with "$1". Using [^\r\n] instead of . keeps the hidden \r of Windows line endings outside the quotes.

Get three guides like this every month, plus software update notices.Subscribe →