Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read user given file character by character in bash

Tags:

bash

shell

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!!

like image 841
NoobEditor Avatar asked Feb 23 '14 13:02

NoobEditor


People also ask

How do you read characters in bash?

Bash read SyntaxThe read command takes the user input and splits the string into fields, assigning each new word to an argument. If there are fewer variables than words, read stores the remaining terms into the final variable. Specifying the argument names is optional.

What does $@ do bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

What does %% mean in bash?

So as far as I can tell, %% doesn't have any special meaning in a bash function name. It would be just like using XX instead. This is despite the definition of a name in the manpage: name A word consisting only of alphanumeric characters and under- scores, and beginning with an alphabetic character or an under- score.

What is $_ in bash?

$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails.


2 Answers

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.

like image 87
devnull Avatar answered Sep 24 '22 20:09

devnull


bash adds a -n flag to the standard read command to specify a number of characters to read, rather than a full line:

while read -n1 c; do
    echo "$c"
done < $file
like image 25
chepner Avatar answered Sep 24 '22 20:09

chepner