Read user given file character by character in bash

bash, shell

Solution

I want to place a new-line after every 100th character and remove any other new lines in it so that file may look with consistent width and readable

Unless you have a good reason to write a script, go ahead but you don't need one.

Remove the newline from the input and fold it. Saying:

tr -d '\n' < inputfile | fold -w 100

should achieve the desired result.

Problem

I have a file which is kind of unformatted, I want to place a new-line after every 100th character and remove any other new lines in it so that file may look with consistent width and readable This code snippet helps read all the lines ``` while read LINE do len=${#LINE} echo "Line length is : $len" done < $file ``` but how do i do same for characters Idea is to have something like this : (just an example, it may have syntax errors, not implemented yet) ``` while read ch #read character do chcount++ # increment character count if [ "$chcount" -eq "100" && "$ch"!="\n" ] #if 100th character and is not a new line then echo -e "\n" #echo new line elif [ "$ch"=="\n" ] #if character is not 100th but new line then ch=" " $replace it with space fi done < $file ``` I am learning `bash`, so please go easy!!

Original source