Issue in reading file line by line

I have a file something like:

a

&nbsp&nbsp&nbsp

b c

&nbsp&nbsp

d
sdfsdf

&nbsp&nbsp

f f

&nbsp

f
f

&nbsp

fffff
dfdf

Now when i read it as

while read line
do
echo $line
done< file

I get the o/p as

a b c d
sdfsdf f f f
f fffff
dfdf

i.e the spaces are trimmed. I want to retain the spaces as well.
Would appreciate help. want to avoid awk.

Thanks

What spaces? I don't see any spaces in your example :rolleyes:

To keep the forums high quality for all users, please take the time to format your posts correctly.

  1. Use Code Tags when you post any code or data samples so others can easily read your code.
    You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags and by hand.)
  2. Avoid adding color or different fonts and font size to your posts.
    Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.
  3. Be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
Reply With Quote

The original file is

a<space><space><space>b<space> c<space><space> d
sdfsdf f<space><space> f f
f<space><space><space> fffff
dfdf

---------- Post updated at 09:40 PM ---------- Previous update was at 09:07 PM ----------

Original file

a	b    c d
sdfsdf	f   f	f
f     ffff
dfdf
# while read line;do echo "$line";done < file
a       b    c d
sdfsdf  f   f   f
f     ffff
dfdf

Double quotes :rolleyes:

PS: Don't forget to edit your first post and add [CODE] tags for data sample and script code.

echo will echo all arguments using argument delimeter and newline.

echo $line

  • shell look the line, if there is variable =>
echo a       b     c d
  • between a, b, c and d has only whitespaces=argument delimeter, not space.
  • then shell call echo using argument a, b, c, d => output is:
    a b c d

echo "$line"

  • shell look the line, is there variable =>
echo "a       b     c d"
  • echo get only one argument
while read line
do

        echo "$line"
        # is not same as
        echo $line
done < file

Thanks everyone...It works that way...