27

I have a data file with a fixed record length. There are no carriage returns or line feeds delimiting the records. How can I insert a carriage return linefeed pair at every X characters using Notepad++(where X is the record length)?

2
  • What is the size of your input file? Nov 22, 2010 at 22:01
  • @idealmachine: The file I am working on is 486 KB with 750 character records.
    – DaveB
    Nov 23, 2010 at 0:25

5 Answers 5

76

I appreciate that it's not ideal, but I'm surprised no one offered this as a pure N++ solution

In a regular expression find/replace

Find:

(.{750})

Replace:

$1\r\n

Roughly translated as...

Find:

750 instances of any character and remember the characters.

Replace:

The 750 characters we just remembered followed by a new line.

Although, to be honest, I'd stick to the powershell approach for anything more than a one-off run.

2
  • 2
    Google brought me here. This worked great for a one-off document I had to split and diff! Thank you very much.
    – bgStack15
    Aug 27, 2014 at 18:40
  • 4
    This! ...is the answer.
    – RLH
    Jan 29, 2016 at 12:56
16

This isn't a task suited to Notepad++, particularly with large input files. Using Windows PowerShell (available for free from Microsoft for Windows XP and Windows Vista, and included with Windows 7), you could do this with:

(gc in.txt) -replace ".{750}" , "$&`r`n" | sc out.txt
2
  • 4
    I am new to PowerShell. Could you break down the various elements of the command and comment what they do?
    – DaveB
    Nov 24, 2010 at 23:13
  • @DaveB 1. Parens are evaluated first, wherein gc command gets content from the file (in.txt). 2. replace command does a replace, taking first argument ("." for any character, {750} for 750 times), and replacing with second (found string ($) and tacks on carriage return+line feed (rn)). 3. This is then sent aka piped ("|") to the next statement. 4. sc command then sets content to the file parameter (out.txt).
    – troy
    Dec 4, 2018 at 19:27
6

Notepad++ v5.8.3 has this command: first select the whole file; then

Edit -> Line Operations -> Split Lines

This splits lines so that they fit exactly in the current window. As far as I can see, you have to use the mouse to resize the window to the width that you want.

2
4

For V6.3

  1. Frame Window to Text Length

  2. Highlight [ALL Text] >>Edit > Select ALL

  3. Edit> LineOperations > SplitLine

Save Document under NEW NAME.

1
  1. Type the number of characters you want on each line, select it, and then choose Edit → Cut.

  2. Edit → Select All, then TextFX → TextFX Edit → ReWrap Text to (Clipboard or 72) width

  3. TextFX → TextFX Edit → Trim Trailing Spaces

2
  • My records are 750 characters. After following your instructions, I am left with records of various lengths. Testing with a length of 10 characters leaves me with lines as short as 1 after trimming off the trailing spaces :( I am using version 5.7 of Notepad++.
    – DaveB
    Nov 22, 2010 at 21:53
  • This doesn't work for me. It always wraps to 72 characters regardless of what's in the clipboard
    – endolith
    Jan 14, 2016 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.