Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a new line character after a fixed number of characters in a file

Tags:

bash

sed

I am looking for a bash or sed script (preferably a one-liner) with which I can insert a new line character after a fixed number of characters in huge text file.

like image 343
rangalo Avatar asked Jul 27 '09 08:07

rangalo


People also ask

How do you insert a line break after every 80 characters?

Usage: var arrayOfLines = fold(longString, 80); var foldedString = arrayOfLines. join('<br/>'); Working jsFiddle.

How do I add a new line after a string?

In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.

How do you write a newline character?

There are two basic new line characters: LF (character : \n, Unicode : U+000A, ASCII : 10, hex : 0x0a): This is simply the '\n' character which we all know from our early programming days. This character is commonly known as the 'Line Feed' or 'Newline Character'.


1 Answers

How about something like this? Change 20 is the number of characters before the newline, and temp.text is the file to replace in..

sed -e "s/.\{20\}/&\n/g" < temp.txt 
like image 198
Kristian Avatar answered Sep 29 '22 11:09

Kristian