The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
ksh - read file with leading spaces momi Shell Programming and Scripting 2 03-17-2008 11:02 AM
Preventing whitespace to be a delimiter in a for loop (bash/sh) kkkoehne Shell Programming and Scripting 2 03-07-2008 03:28 PM
ls while read loop - internal read picking up wrong input dkieran Shell Programming and Scripting 2 05-14-2007 12:02 PM
While read loop and rsh 104234 UNIX for Advanced & Expert Users 1 01-15-2006 07:53 AM
read inside a while loop dta4316 UNIX for Dummies Questions & Answers 3 05-21-2005 07:53 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-07-2004
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
while read loop preserving leading whitespace

Hi all,

I've been trying to get this to work for ages to no avail. I've searched this site and googled but cannot find a satisfactory answer.

I've got a while loop, like this
Code:
while read line
do
    echo "$line"
done < file_name
Now, my problem is that most of the lines in the file have indentation (this program parses indented source code).

The indentation is lost during the loop and everything appears left-justified.

Now, am I missing something *really* silly here?

Peace,
ZB
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-07-2004
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,243
Just change the value of IFS, e.g....
Code:
( IFS='\n' 
  while read line
  do
      echo "$line"
  done < file_name )
Reply With Quote
  #3 (permalink)  
Old 06-07-2004
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Many thanks Ygor, I thought it'd be something fairly simple!

Cheers
ZB
Reply With Quote
  #4 (permalink)  
Old 06-07-2004
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Just thought I'd let everyone know that I decided to implement this in awk in the end, as processing the syntax of a gazillion shell scripts is easier to do with awk. As $0 can just be processed the way I wanted leaving whitespace intact using "\n" as the RS.

I am writing a script which will convert shell syntax to colour-highlighted HTML. I found scripts to do this for just about every language EXCEPT for humble old SH so I decided to do it myself! You can view a sample output from the script HERE . The script itself is still under test and thus is not yet online.

Sorry for the "shameless plug" but I thought I'd let everybody know what the purpose of my original post was.

Peace
ZB

Last edited by zazzybob; 06-07-2004 at 02:54 PM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:28 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0