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

1 Answers1

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"
cuonglm
  • 153,898