11 Vote

Rewrite Text Files with a fixed Line Length

Tutorial by Stefan Trost | 2022-03-13 at 15:54

In most text files, line breaks are realized via a character, which stands for a line break. Depending on the system and the scope of application, this character can be different or even several characters can be used, for example LF (#0A) on Linux or macOS systems or CR LF (#0D #0A) on Windows. The program that displays the text file hides these characters and instead represents a break of the line. An overview of usual line bracket types can be found on this page.

However, there is also another way to realize line breaks in text files that comes without characters: the fixed line length. In this case, all lines of the text are written in succession and the definition of a line results from a predetermined number of characters after which there is always a break. This type of line break is sometimes used for CSV files, in which always a fixed number of values appear per line.

Example of a Text File with fixed line length

An example of such a fixed line length file (here 6 characters per line) could look like this:

abcdefabcdefabcdefabcdef...

We want to convert this file and use the line break type of our system instead of the fixed line length, so that the file can also be viewed in an average text editor.

The result should look like this after the conversion:

abcdef
abcdef
abcdef
abcdef
...

Convert Files with fixed Line Length into files with Line Break Character

We are using the program TextEncoder for the conversion, with which text files with a fixed line length can be rewritten into any other line break format.

  1. First, we open the files we want to edit in the TextEncoder. For that, we can just drag the files onto the program. You can edit any number of files simultaneously.
  2. Then we activate the option "Line Breaks" on the right side under "Changes". Here we can set the type of line break, the files should be read in and what kind of line break the files should be stored.
  3. From the drop-down box under "Read as" we select "Line break after this number of characters (fixed line length)". In the text box underneath, we can enter how many characters our lines should have (for example, 6 for our example above).
  4. From the drop-down box under "Save as" we select "System line break". That would be CR LF on Windows. But we can also select any other type or even define our own custom characters as a line break.
  5. Under "Storage Options" we can then specify whether we want to override the original file or save the result of the conversion as a new file.
  6. Finally, we can click on the button "Convert and Save" and thus rewrite all files in the list according to our settings.

Convert Files with Line Break Character to Files with fixed fixed Line Length

Of course, we can also use the TextEncoder for converting text files into the inverted direction. For this, we can enter the currently used line break type of our file under "Read as" ("Auto Detect" is our friend) and choose the option "No Character" under "Save as".

With these settings, all existing line break characters are deleted from our original file and we get a file with a fixed line length. Prerequisite for this is, of course, that the existing lines were already all of the same length.

Using the Command Line

With the batch version of the TextEncoder, all mentioned tasks can be accessed alternatively via the command line so that you can control the conversion via script.

With the following call we convert the file test.txt. The file is read in with a fixed row length of 10 characters per line (lb-read=fixedlength-10), while the file is stored with the Windows line break (lb=crlf).

TextEncoder.exe -cl test.txt lb-read=fixedlength-10 lb=crlf

The other way round you can use the parameter lb=nochar:

TextEncoder.exe -cl test.txt lb=nochar

With this call, all line break characters from the file test.txt are removed. In this case, we omit the parameter lb-read, thereby an automatic detection of the line break of the file is done. Of course, we could also write lb-read=crlf or the like to enforce a specific line break type when reading.

An introduction to the batch functionality of the TextEncoder as well as an overview of all available parameters can be found on this page.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.