0

I am reading a file using the following code:

displayLine(){
    echo $line
}

filename="SampleFile"
while read line
do
    displayLine $line
done < "$filename"

The format of the file that I am getting after using the script is this:

ID EVENT OK NOK
101 ABC1123 ok nok
101 ABC1223 ok      
101 ABC1323 ok nok
101 ABC1423 ok nok

But the actual format of the file is like this:

ID  EVENT       OK      NOK
101 ABC1123     ok      nok
101 ABC1223     ok      
101 ABC1323     ok      nok
101 ABC1423     ok      nok

My script is somehow trimming the extra spaces between the words. However, I want the actual format of the file.

Could anyone tell me how to achieve that?

Thanks!!

3
  • why do you need a separate function to echo one thing ? Commented Feb 11, 2016 at 18:38
  • The function is suppose to do something else. But for the time being I am using it for echo. Commented Feb 11, 2016 at 18:44
  • Related: unix.stackexchange.com/q/169716/38906 Commented Feb 12, 2016 at 8:00

1 Answer 1

1

Quote your variables and use IFS= with read, as this:

displayLine(){ printf '%s\n' "$line"; }

filename="infile"

while IFS= read -r line
do
    displayLine "$line"
done < "$filename"

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.