Preserve leading white space

I have about 350 programs in which I have to add 2 lines; one before and one after a specfic line.
The following script does the job except that I lose the indentation.

#!/usr/bin/bash                                                        
while read line                                                        
do                                                                     
        line1=$line                                                    
        if [ "${line1:0:4}" = "@1,0" ]                                 
        then                                                           
        echo "if file (\"/u2/email/eps/\"+alltrim(writer)+\".eps\")"   
        echo "$line"                                                   
        echo "endif"                                                   
        else                                                           
        echo "$line"                                                   
        fi                                                             
done      

I assume the setting of line1=$line is for compatibility reasons for within the existing scripts?
You keep using $line though...
If the setting of line1 is required, i'd put some qoutes around it.

What idention do you loose?
You check for the line/1 to begin with "@1,0" - so there is no idention.

---------- Post updated at 22:45 ---------- Previous update was at 22:40 ----------

Figured, you probably ment to ident the $line in the output, change to

        echo "    $line"                                                   
        echo "endif" 

hth

I moved $line to $line1 so that any leading white space would be removed.
I have tried changing IFS to a tilde, but it makes no difference. The programs have indented code for readability

before

if fax_letter = '     '
        bold_on=""   
        bold_off=""  
endif   

after

if fax_letter = '     '  
bold_on=""             
bold_off=""            
endif    

At least embedded blanks are preserved.

The assignment line1=$line does the assignment, no other treatment.
You need IFS="" read , environment only for read (inheritance from outside the loop does not work for some reason).
Further, read pastes lines together that end with \ .
So often you find

while IFS="" read -r line

This is much closer.

#!/usr/bin/bash                                                       
IFS="~"                                                               
while read line                                                       
do                                                                    
        IFS=                                                          
        line1=$line                                                   
        while [ "${line1:0:1}" = " "  -o "${line1:0:1}" = "     " ]   
        do                                                            
        line1=${line1:1}                                              
        done                                                          
        if [ "${line1:0:4}" = "@1,0" ]                                
        then                                                          
        echo "if file (\"/u2/email/eps/\"+alltrim(writer)+\".eps\")"  
        echo "$line"                                                  
        echo "endif"                                                  
        else                                                          
        echo "$line"                                                  
        fi                                                            
        IS="~"  

With the exception that the backslash in the line below was lost.

# diff d1.prg /u1/ltrs2/d1.prg                     
4a5                                                
> if file ("/u2/email/eps/"+alltrim(writer)+".eps")
6a8                                                
> endif                                            
49c51                                              
< @lnum,06 say '\s1'                               
---                                                
> @lnum,06 say 's1'    

read interprets backslashes by default, a little-known and occasionally baffling behavior. read -r to avoid that.

IFS="" while read -r line
do
...
done
# bash --version                                    
GNU bash, version 3.2.17(1)-release (i586-pc-sysv5) 
Copyright (C) 2005 Free Software Foundation, Inc.   
#      

Running on SCO Openserver 6.0.0 in case it makes any difference.

---------- Post updated at 05:45 PM ---------- Previous update was at 05:37 PM ----------

Success.

#!/usr/bin/bash                                                        
IFS="~"                                                                
while read -r line                                                     
do                                                                     
        IFS=                                                           
        line1=$line                                                    
        while [ "${line1:0:1}" = " "  -o "${line1:0:1}" = "     " ]    
        do                                                             
        line1=${line1:1}                                               
        done                                                           
        if [ "${line1:0:4}" = "@1,0" ]                                 
        then                                                           
        echo "if file (\"/u2/email/eps/\"+alltrim(writer)+\".eps\")"   
        echo "$line"                                                   
        echo "endif"                                                   
        else                                                           
        echo "$line"                                                   
        fi                                                             
        IS="~"                                                         
done                                                                   
#        

Having the following line produced a run time error

IFS="~" while read -r line
  # ./chg.bash <d1.prg >/u1/ltrs2/d1.prg                       
./chg.bash: line 2: while: command not found                
./chg.bash: line 3: syntax error near unexpected token `do' 
./chg.bash: line 3: `do'                                    
#                                      

6 more years and that BASH will be old enough to drive :slight_smile:

6 more years and I will be too old to drive. :smiley: