How can I pad every line in a text file with a given number of spaces with uneven length lines?

5,567

Using Notepad++ it is possible to perform two regex based replaces that will pad all lines with spaces, and then trim all lines to the same length. I have not tested the performance of this technique against a 40,000 line file.

The first replace is going to add spaces to the right of the text:

enter image description here

In the replace tab set "Regular expression" and make sure that "matches newline" and "Wrap around" are off. Type ^(.*) which means match everything on the line into "Find what" and type $1 followed by the number of spaces you want to add (in your example add 9).

After clicking replace all lines should have at least the desired length and many will be longer than you want.

The second replace, which trims. enter image description here

For the second replace we are going to grab two parts to the line and throw one away.

First make sure that "Replace with" only has $1 in it and no spaces after. Then change "Find what" to ^(.{28})(.*) which reads find from the beginning of the line the first 28 (change to 31 to keep 3 spaces as padding) characters into the first group, and then find everything else on the line into the second group.

When this is applied with "Replace" it should trim all the lines to the same length.

Note: some regex engines might need $ which means "the end of the line" appended when using this technique.

Share:
5,567

Related videos on Youtube

Claudio
Author by

Claudio

Updated on September 18, 2022

Comments

  • Claudio
    Claudio almost 2 years

    I have this text file which I'm trying to sort out, it's got characters such as below:

    abcd  a27382  ESH738 991839
    ahsj  dhaksj  GSH736 774828
    ahsj  dhaksj  RHA330
    ahsj  dhaksj  GSH736 774828
    

    My question is, how can I insert 3 spaces after the last set of numbers on the right side across all the lines without notepad doing the carriage return when I get to the end of the last number?

    I need to do this for over 40,000 rows so manually pressing the spacebar at the end of each line to get those 3 extra spaces in, is out of the question. Also in the places where there's no number (example, row 3) I still need those extra blank spaces to make up for the missing numbers, in that case, it would be 9 spaces (6 for the non existing numbers and 3 for the last spaces after). Please let me know if you know of any easy way to do this as it would be of great help, thanks!

    • Jason Aller
      Jason Aller about 9 years
      Are you looking for a Windows specific solution? Do you have access to Excel? Do you have any scripting languages or Notepad++ installed?
    • Claudio
      Claudio about 9 years
      @Jason Aller - Yes, I have Notepad++ and yes, Windows specific solution.
  • Claudio
    Claudio about 9 years
    I need to edit the file, where do I actually type that code you provided?
  • Claudio
    Claudio about 9 years
    Thanks Jason! I've sorted it out in SQL in the meantime(imported it and then added the spaces using SQL code), however, your solution also works.