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

John Rambo
  • 906
  • 1
  • 17
  • 37
  • How are you viewing the file? What does it look like if you `cat` it? – Dan Feb 11 '16 at 18:34
  • @dan08 It looks exactly like the actual file. I have viewed the file using both vi editor and cat command. They show the file with correct format. However, when I use the aforementioned script, it removes the extra spaces between the words. – John Rambo Feb 11 '16 at 18:37
  • @KarolyHorvath Thank!! It works – John Rambo Feb 11 '16 at 18:42
  • Sidenote: Read up about functions, you're abusing them. – Karoly Horvath Feb 11 '16 at 18:43
  • @KarolyHorvath yeah sure, I am new to shell scripting. I am just having some hands on calling functions with parameter. – John Rambo Feb 11 '16 at 18:46
  • 1
    Possible duplicate of [I just assigned a variable, but echo $variable shows something else](http://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) – Benjamin W. Feb 11 '16 at 19:12

1 Answers1

1

Try this in a console window:

echo    101 ABC1123     ok      nok

What do you get?

Now try

echo "101 ABC1123     ok      nok"

Now, try echo "$line" in your script.

gnac
  • 1,573
  • 15
  • 17