HELP: How do I read a file line-by-line without getting spaces stripped?

HELP: How do I read a file line-by-line without getting spaces stripped?

Post by Stephen Bernar » Fri, 09 Feb 1996 04:00:00



I am writing a sh shell script and I need to read a file line-by-line into
a variable without changing the lines. I am using a While loop as shown below
but it is stripping off leading spaces and multiple spaces in the line it is
reading in! Does anyone know a way to read in a file line-byline without
modifying the line?

Script currently being used:

while read j
do

         tc_title=$j  # store the TC title line in variable tc_title
         echo "$tc_title" >> $temp_dir/temp_9

done < $tc_files_dir/$i

Thanks for any help.

 
 
 

HELP: How do I read a file line-by-line without getting spaces stripped?

Post by Bill Marc » Sat, 10 Feb 1996 04:00:00



Quote:>I am writing a sh shell script and I need to read a file line-by-line into
>a variable without changing the lines. I am using a While loop as shown below
>Script currently being used:

>while read j
>do
>         tc_title=$j  # store the TC title line in variable tc_title

         tc_title="$j"  # store the TC title line in variable tc_title
Quote:>         echo "$tc_title" >> $temp_dir/temp_9

>done < $tc_files_dir/$i


 
 
 

HELP: How do I read a file line-by-line without getting spaces stripped?

Post by Bruce Bli » Sat, 10 Feb 1996 04:00:00


: I am writing a sh shell script and I need to read a file line-by-line into
: a variable without changing the lines. I am using a While loop as shown below
: but it is stripping off leading spaces and multiple spaces in the line it is
: reading in! Does anyone know a way to read in a file line-byline without
: modifying the line?

The read command uses the characters stored in the IFS variable to parse its
input.  IFS usually contains space, tab, and newline, which causes the read
command to strip whitespace from its input.  You can set the IFS variable to
empty to prevent this from happening; however, the IFS variable is used for
many other things as well, so it is best to restore its normal value where
possible.  For example:

OLDIFS=IFS
IFS=
while read LINE
do
        IFS=$OLDIFS

        echo "$LINE"

        IFS=
done
IFS=$OLDIFS

Bruce Blinn

 
 
 

HELP: How do I read a file line-by-line without getting spaces stripped?

Post by Ashvin Rekha » Tue, 13 Feb 1996 04:00:00


|> I am writing a sh shell script and I need to read a file line-by-line into
|> a variable without changing the lines. I am using a While loop as shown below
|> but it is stripping off leading spaces and multiple spaces in the line it is
|> reading in! Does anyone know a way to read in a file line-byline without
|> modifying the line?
|>
|> Script currently being used:
|>
|> while read j
|> do
|>
|>          tc_title=$j  # store the TC title line in variable tc_title
|>          echo "$tc_title" >> $temp_dir/temp_9
|>
|> done < $tc_files_dir/$i
|>
|>
|>
|> Thanks for any help.
|>

Try the following:

#--set counter for sed
i=1

while true
do
        VAR=`sed -n ''$i'p' $FILENAME`
        if [ "$VAR" = "" ]  #---break if line has no chars or EOF
        then
                break
        else
                #---do file processing here
        #---quotes ensure that line echoes with initial spaces
        echo "$VAR"  

        #---don't forget to increment the counter
        i=`expr $i + 1`
        fi
done

This will echo one line of FILENAME at a time, progressing top to bottom.
Hope this helps.

Regards,
Ashvin.

==========================================================================

 
 
 

HELP: How do I read a file line-by-line without getting spaces stripped?

Post by Brian Duan » Fri, 16 Feb 1996 04:00:00





> : > I am writing a sh shell script and I need to read a file line-by-line into
> : > a variable without changing the lines. I am using a While loop as shown below
> : > but it is stripping off leading spaces and multiple spaces in the line it is
> : > reading in! Does anyone know a way to read in a file line-byline without
> : > modifying the line?
> : >
> : > Script currently being used:
> : >
> : > while read j
> : > do
> : >
> : >          tc_title=$j  # store the TC title line in variable tc_title
> : >          echo "$tc_title" >> $temp_dir/temp_9
> : >
> : > done < $tc_files_dir/$i

> : I can't pull up my Unix system to try it, but I think I see the problem:

> : You have:

> :     tc_title=$j                 #No Quotes

> : Try:

> :     tc_title="$j"               #The quotes around the $j should prevent the
> :                                 #shell from removing the spaces

> I'm afraid that won't work. The problem is that the shell "read" tokenises
> the input line and then strings the tokens together in the variable "j".

> If you set the IFS (Input Field Separator) to some "odd" character before
> the read you may sort it out... here is an example :-

> #/bin/sh
> old_ifs=$IFS
> IFS=`echo '\007'`               # control-G as delimiter, so
>                                 # whole lines are taken as one field
> while read j
> do
>         echo "$j"
> done < fred
> IFS=$old_ifs

> --
> Mark Bluemel    Unix/Oracle Trainer and Consultant
>                 My opinions are my own, but I'll share them
>                 All solutions to problems are offered "as is"
>                 and without warranty - you have been warned :-)

while read j
do
    blah "$j" blah
done < filename

... does preserve embedded tabbing/spacing (at least it does on my system) however
leading tabbing/spacing is dropped

To preserve ALL tabbing/spacing, try the following:
while j=`line`
do
    blah "$j" blah
done < filename
--
Brian Duane - NCR Corporation
(Formerly AT&T GIS, formerly NCR)

 
 
 

HELP: How do I read a file line-by-line without getting spaces stripped?

Post by Zefr » Sun, 18 Feb 1996 04:00:00



>IFS=`echo '\007'`           # control-G as delimiter, so

That's not portable.  Not all echos treat \ that way.  For example, on
this system:

$ echo \\007
\007

The portable way to do it is

IFS=^G

using a literal ^G, of course.  (I would include one, but inews doesn't
like it.)

-zefram

 
 
 

1. line by line without using echo "$i" | while read line?

i=$(ps -ef)
and
I do not want to use
echo "$i" | while read line
any options?

The reason I do not want to use "| while"
is that it will run a subshell:

pipe()                                          = 3 [4]
fork()                                          = 1089
    Received signal #18, SIGCLD [caught]
      siginfo: SIGCLD CLD_EXITED pid=1089 status=0x0000

And the environment variable defined in the while loop
can not get out of the loop in Borne shell.

I tried

IFS="\n"
for j in $i

but it does not work.

Thanks.

2. Does HD cache help Linux?

3. How to read lines from a file without doing a loop ?

4. disk-on-chip support

5. read from stdin while reading line by line a file

6. shared libraries under AIX-4.2 once again

7. line by line reading of the lines in list

8. Any 'top' like graphic tool?

9. Insert a line at the first line without a # in a file

10. getting rid of top lines of a file without searching through entire file

11. Read without supression of spaces at the beginning of line

12. How to read line by line of a file

13. read a file line by line