while read loop preserving leading whitespace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting while read loop preserving leading whitespace
# 1  
Old 06-07-2004
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
# 2  
Old 06-07-2004
Just change the value of IFS, e.g....
Code:
( IFS='\n' 
  while read line
  do
      echo "$line"
  done < file_name )

# 3  
Old 06-07-2004
Many thanks Ygor, I thought it'd be something fairly simple!

Cheers
ZB
# 4  
Old 06-07-2004
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 06:54 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

REGEX to separate paths by whitespace and do a loop

I am trying to do in a single line to take a list of paths separated by whitespace and then loop thru all the paths that were wrote but my regex is not working, I have echo {3} | sed 's/ //g' | while read EACHFILE do ..... But for some reason is only taking always the first path that I... (7 Replies)
Discussion started by: jorgejac
7 Replies

2. Shell Programming and Scripting

[BASH] read 'line' issue with leading tabs and virtual line breaks

Heyas I'm trying to read/display a file its content and put borders around it (tui-cat / tui-cat -t(ypwriter). The typewriter-part is a 'bonus' but still has its own flaws, but thats for later. So in some way, i'm trying to rewrite cat using bash and other commands. But sadly it fails on... (2 Replies)
Discussion started by: sea
2 Replies

3. Shell Programming and Scripting

Read line, issue with leading - and {}'s

Heyas With my forum search term 'issue with leading dash' i found 2 closed threads which sadly didnt help me. Also me was to eager to add the script, that i didnt properly test, and just now figured this issue. So i have this code: if ] then while read line do line="${line/-/'\-'}"... (7 Replies)
Discussion started by: sea
7 Replies

4. UNIX for Dummies Questions & Answers

[Solved] How remove leading whitespace from xml (sed /awk?)

Hi again I have an xml file and want to remove the leading white space as it causes me issues later in my script I see sed is possible but cant seem to get it to work I tried sed 's/^ *//' file.xml output <xn:VsDataContainer id="1U104799" modifier="update"> ... (10 Replies)
Discussion started by: aniquebmx
10 Replies

5. Shell Programming and Scripting

Preserving values with for loop for later use

Below is the issue I am having. I have a few variables which have certain values in them like var1=23 var2=46 var3=78 etc... I want to save these values with the help of a for loop in a single variable so that I can use it later,beacuse a few lines down the script, some of these... (3 Replies)
Discussion started by: Elizabeth H
3 Replies

6. Shell Programming and Scripting

How to match (whitespace digits whitespace) sequence?

Hi Following is an example line. echo "192.22.22.22 \"33dffwef\" 200 300 dsdsd" | sed "s:\(\ *\ \):\1:" I want it's output to be 200 However this is not the case. Can you tell me how to do it? I don't want to use AWK for this. Secondly, how can i fetch just 300? Should I use "\2"... (3 Replies)
Discussion started by: shahanali
3 Replies

7. Shell Programming and Scripting

Preventing whitespace to be a delimiter in a for loop (bash/sh)

Hi, I have a for loop which iterates over a list of strings, separated by whitespace: $ list="1 2 3" $ for i in $list; do echo $i; done 1 2 3 I now want to introduce some strings containing whitespace themselves ... This is straightforward if I directly iterate over the list: $ for... (4 Replies)
Discussion started by: kkkoehne
4 Replies

8. Shell Programming and Scripting

Whitespace in filenames in for loop in bash script

I'm trying to search all .odt files in a directory for a string in the text of the file. I've found a bash script that works, except that it can't handle whitespace in the filenames. #!/bin/bash if ; then echo "Usage: searchodt searchterm" exit 1 fi for file in $(ls *.odt); do ... (4 Replies)
Discussion started by: triplemaya
4 Replies

9. Shell Programming and Scripting

Preserving whitespace in a for loop

I obviously haven't learned my lesson with shell and whitespace. find /path/to/some/where/ -name "*.pdf" | awk '{print $5}'| uniq -d results: some Corporation other Corporate junk firmx Works fine from cmdline but the whitespace turns into another FS in a for loop. for... (7 Replies)
Discussion started by: s_becker
7 Replies

10. Shell Programming and Scripting

ksh - read file with leading spaces

Hi, Could someone has any suggestions on this? When read a line from a file, I need to check the first char in the line, it could be a space or any char. But the leading spaces are removed by read. Thanks. (2 Replies)
Discussion started by: momi
2 Replies
Login or Register to Ask a Question