Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Placing every character on a new line

Tags:

I have a file like this:

This is a sentence. This is another sentence. 

I need to put a new line after each character, such that only one character appears on every line, e.g.:

T h i s  i s  a  s e n t e n c e . T h i s  i s  a n o t h e r  s e n t e n c e . 
  • The file is in UTF-8 and contains many non-English characters.
  • It does not matter if spaces or carriage returns have their own line.

How can I remove every character to a new line?

like image 204
Village Avatar asked Mar 27 '12 23:03

Village


People also ask

How to put text on a new line in Python?

The new line character in Python is \n . It is used to indicate the end of a line of text. You can print strings without adding a new line with end = <character> , which <character> is the character that will be used to separate the lines.

How to go in new line in JavaScript?

The newline character is \n in JavaScript and many other languages. All you need to do is add \n character whenever you require a line break to add a new line to a string.

Is new line two characters?

It is a character in a string which represents a line break, which means that after this character, a new line will start. 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.

How to add new line in typescript?

To add a new line in string in typescript, use new line \n with the + operator it will concatenate the string and return a new string with a new line separator. put the "\n" where you want to break the line and concatenate another string.


Video Answer


2 Answers

Using sed replace every character with itself followed by a newline:

sed 's/./\0\n/g' -i filename 
like image 123
Paul Avatar answered Oct 06 '22 23:10

Paul


  • sed $'s/./&\\\n/g' (with BSD sed)
    • Or sed 's/./&\n/g' with GNU sed
    • Doesn't include empty lines for linefeeds
  • fold -w1
    • -w specifies width in characters
    • Doesn't include empty lines for linefeeds
  • while IFS= read -r -n1 -d '' c; do printf %s\\n "$c"; done
    • Includes empty lines for linefeeds with -d ''
    • The only option for read specified by POSIX is -r
  • gawk -F '' 'OFS="\n"{$1=$1}1'
    • Or awk 'BEGIN{FS="";OFS="\n"}{$1=$1}1' in nawk (BSD awk, the awk that comes with OS X); it doesn't work with multibyte characters though
    • Neither includes empty lines for linefeeds

All except the nawk command worked with non-ASCII characters in my environment when LC_CTYPE was set to a UTF-8 locale. None collapsed or stripped spaces.

like image 27
Lri Avatar answered Oct 06 '22 23:10

Lri