Find the answer to your Linux question:
Results 1 to 3 of 3
I am trying to read a file character wise and trying to write the same character to another file. In this process, I unable to read and write white spaces ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2010
    Posts
    8

    Reading And Writing White Spaces to a File


    I am trying to read a file character wise and trying to write the same character to another file. In this process, I unable to read and write white spaces successfully to the new file. The script reads the white spaces but while writing the white space is lost. The section of the code, is given below. Please advice how can i read and retain the white space while writing to a new file.
    Code:
    if [ -s f_test.txt ] && [ -f f_test.txt ]; then
           echo "File Exists  !!"
           while read -n1 char; do
                      echo -n "$char"
                       # print -n "$char" >> 2dec_out.txt
           done < f_test.txt
    
    else
    
          echo "File Doesn't Exist - Script Terminates."
          exit 1
    
    fi

  2. #2
    Linux User Manko10's Avatar
    Join Date
    Sep 2010
    Posts
    250
    Ist this an exercise? If not I would just recommend using cp.
    Refining Linux Advent calendar: 24 Outstanding ZSH Gems

  3. #3
    Just Joined!
    Join Date
    Jul 2010
    Posts
    8
    Manko,

    thanks for the suggestion. this is actually a part of a bigger problem and I have broken down into several small modules and this part is one among them.

    The problem statement goes like this " There a text file with lot of special characters in it. program must idetify the second double quotes that comes after every 41st pipe "|" character and introduce a line break after that character".

    To solve this i read the file character wise and apply some logic and write it to a file and in that process, I am stuck with the above issue. Now that i have completed all other tasks, the output file is fine but for the whitespaces collapsed, where i would need a whitespace to displayed.

    I have found out some additional ways to fix this and will request help again if I am not able to make it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •