Register
It is currently Wed Jul 30, 2014 5:16 pm

operate on lines in a file preserving leading spaces


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Fri Jan 16, 2009 8:44 am   

Joined: Fri Dec 05, 2008 11:01 am
Posts: 6
Hi.

I want to apend the string "<br>" to each line of a file and echo it to stdout. Many of the lines have leading spaces which I want to preserve in the ouptut. Here was my attempt...

cat filename | while read a; do echo $a \<br\>; done

HOWEVER, the leading spaces are stripped from any lines that have them. When I read more about the "read" command it seems to make sense because as I understand it read is tokenizing the words in the lines based on white space it finds in them, thus discarding all white space.

So, how can I accomplish this preserving leading spaces?

Thanks!


Top
 Profile  
 PostPosted: Fri Jan 16, 2009 10:42 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
First of all, since you're doing html, are you trying to keep indenting in the source or do you want the spaces to be shown on the page?

I'll write the solution for both,
If you just want it to be for source indenting then just encase your echo in quotation
Code:
echo "$a <br>"


if you want the spaces to be there for the output of a browser you need to do convert the spaces to html entities. This because of html's way of interpret whitespaces.
Code:
echo "$a <br>" | sed -e "s/ /&nbsp;/g"


Best regards
Fredrik Eriksson


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP