How to Remove the First (or Last) N Characters From Each Line

· 5 minutes read

How to Remove the First (or Last) N Characters From Each Line

Copied lists often come with something you don't want at the start or end of every line: numbering like 1., a bullet, a fixed-width code, a date stamp or a trailing ;. Deleting it line by line is tedious. Here is how to remove the first or last N characters from each line – or everything before or after a specific character – with whatever you have.

The fastest way: online, Unicode-safe

Our remove characters from lines tool works directly in your browser:

  1. Open the remove characters from lines tool.
  2. Paste your text.
  3. Set Remove first to the number of characters to drop from the start (1 by default), and/or Remove last for the end.
  4. To cut at a character instead, choose before the marker or after the marker and type it, e.g. : or -.
  5. Copy the result.
1. Ján Novák      →   Ján Novák
2. Noël Dupont    →   Noël Dupont

Unlike most formulas and scripts, the tool counts characters as you see them: an emoji, a flag or a letter with a diacritic is one character and is never cut in half. Lines without the marker are left untouched, and you can choose the first or last occurrence of the marker.

Excel and Google Sheets

With the text in A1 and N being the number of characters:

Goal Formula
Remove first N characters =RIGHT(A1, LEN(A1)-N) or =MID(A1, N+1, LEN(A1))
Remove last N characters =LEFT(A1, LEN(A1)-N)
Remove everything before the first "-" =MID(A1, FIND("-", A1)+1, LEN(A1))
Remove everything after the first "-" =LEFT(A1, FIND("-", A1)-1)

A few things to know:

  • RIGHT and LEFT return an error when LEN(A1)-N is negative, i.e. when the cell is shorter than N. Wrap the formula in IFERROR(…, ""). The MID variant simply returns an empty string.
  • FIND is case-sensitive and returns an error when the character is missing – use IFERROR(…, A1) to keep such cells unchanged.
  • Excel 365 and Google Sheets also have TEXTAFTER(A1, "-") / TEXTBEFORE(A1, "-") in Excel and REGEXREPLACE(A1, "^.{3}", "") in Google Sheets.
  • LEN counts UTF-16 code units, so an emoji counts as 2 and can be split.

Notepad++

Use Search → Replace (Ctrl+H) with Regular expression mode:

  • Remove the first 3 characters: Find ^.{3}, Replace with nothing.
  • Remove the last 2 characters: Find .{2}$, Replace with nothing.
  • Remove everything before the first colon (including it): Find ^[^:]*:\s*, Replace with nothing.
  • Remove everything after the last slash: Find /[^/]*$, Replace with /.

Make sure . matches newline is unchecked, otherwise the pattern can run across lines. For a fixed-width cut you can also use column selection (Alt + drag) and press Delete.

VS Code

Open Find and Replace (Ctrl+H), enable regular expressions (.*) and use the same patterns as above. Alternatively, select all lines, press Shift+Alt+I for a cursor on every line, press Home, then Shift+→ N times and Delete.

Command line

cut -c4- input.txt                 # remove the first 3 characters
sed 's/^.\{3\}//' input.txt        # same with sed
sed 's/.\{2\}$//' input.txt        # remove the last 2 characters
sed 's/^[^:]*: *//' input.txt      # remove everything before the first ": "
sed 's/@.*//' emails.txt           # remove everything after "@"
awk '{print substr($0, 4)}' input.txt

With GNU tools, cut -c counts bytes, not characters, so it splits accented letters and emoji in UTF-8 text. GNU sed and gawk respect the locale – run them with a UTF-8 locale (e.g. LC_ALL=en_US.UTF-8) to count characters correctly. On Windows PowerShell: Get-Content input.txt | ForEach-Object { $_.Substring([Math]::Min(3, $_.Length)) }.

Common pitfalls

  • Invisible leading spaces. If some lines start with a space or a tab, removing the first character removes the space, not the number. Trim lines first (there is an option for it).
  • Different prefix lengths. "9. " has 3 characters but "10. " has 4. In that case cut before a marker (.) instead of a fixed count.
  • Windows line endings. A hidden \r at the end of each line counts as a character for "remove last N". Our tool normalises line endings; for sed, convert with dos2unix.
  • Byte vs. character counts. As above – cut, some editors and spreadsheet LEN can split multi-byte characters.

FAQ

How do I remove the first character from each line?

Paste the text into the tool – removing the first character is the default setting. In a terminal use cut -c2- (ASCII) or sed 's/^.//'.

How do I remove everything before a specific character on each line?

Choose before the marker and type the character. Tick Keep the marker if the character itself should stay. In Excel use =MID(A1, FIND(":", A1)+1, LEN(A1)).

Can I add text to each line afterwards?

Yes – send the result to the add prefix and suffix tool with the "Send result to…" button to add commas, quotes or other text.

Does the online tool store my text?

No. Everything is processed locally in your browser.