Preserve leading white space


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Preserve leading white space
# 1  
Old 02-26-2015
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.
Code:
#!/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

# 2  
Old 02-26-2015
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
Code:
        echo "    $line"                                                   
        echo "endif"

hth

Last edited by sea; 02-26-2015 at 05:47 PM.. Reason: Code fix
# 3  
Old 02-26-2015
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
Code:
if fax_letter = '     '
        bold_on=""   
        bold_off=""  
endif

after
Code:
if fax_letter = '     '  
bold_on=""             
bold_off=""            
endif

At least embedded blanks are preserved.
# 4  
Old 02-26-2015
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
Code:
while IFS="" read -r line

# 5  
Old 02-26-2015
This is much closer.
Code:
#!/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.
Code:
# 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'

# 6  
Old 02-26-2015
read interprets backslashes by default, a little-known and occasionally baffling behavior. read -r to avoid that.
Code:
IFS="" while read -r line
do
...
done

# 7  
Old 02-26-2015
Code:
# 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.
Code:
#!/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
Code:
IFS="~" while read -r line

Code:
  # ./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'                                    
#

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add white space

hi guys how can i add spacein file name with sed if strings have no space around dash input 19-20 ( 18-19 ) ABC-EFG output after add white space 19 - 20 (18 - 19 ) ABC - EFG thx in advance (2 Replies)
Discussion started by: mhs
2 Replies

2. UNIX for Advanced & Expert Users

Need to remove leading space from awk statement space from calculation

I created a awk state to calculate the number of success however when the query runs it has a leading zero. Any ideas on how to remove the leading zero from the calculation? Here is my query: cat myfile.log | grep | awk '{print $2,$3,$7,$11,$15,$19,$23,$27,$31,$35($19/$15*100)}' 02:00:00... (1 Reply)
Discussion started by: bizomb
1 Replies

3. UNIX for Dummies Questions & Answers

filename with white space

our user creates a text file with a white space on the filename. this same file is transfered to unix via automation tool. i have a korn shell script that reads these files on a input directory and connects to oracle database to run the oracle procedures which will load the data from each of the... (2 Replies)
Discussion started by: wtolentino
2 Replies

4. Shell Programming and Scripting

Preserve space in variable of AWK

This seems to be a stupid basic question, but I cant get the space to stick in the awk variable. I do use this command to grep a time range of the log file. cat /var/log/daemon.log | awk '$0>=from&&$0<=to' from="$(date +%b" "%e" "%H:%M:%S -d -24hour)" to="$(date +%b" "%e" "%H:%M:%S)" I now... (9 Replies)
Discussion started by: Jotne
9 Replies

5. Shell Programming and Scripting

sed + white space

Hi, What sed command (if sed is the right command) can remove ALL white space from my file. I have a csv, except I want to remove all white space between commas and characters. My idea (without testing) sed 's/ //g' Is there a better way? (18 Replies)
Discussion started by: mcclunyboy
18 Replies

6. Shell Programming and Scripting

Leading white spaces

Hi, I am having problem in deleting the leading spaces:- cat x.csv baseball,NULL,8798765,Most played baseball,NULL,8928192,Most played baseball,NULL,5678945,Most played cricket,NOTNULL,125782,Usually played cricket,NOTNULL,678921,Usually played $ nawk 'BEGIN{FS=","}!a... (2 Replies)
Discussion started by: scripter12
2 Replies

7. Shell Programming and Scripting

How to preserve space while concatenating strings? (KSH)

I have these str1=$(echo "This is string one with spaces \n This is also my sentence 1") When I echo $str1, it displays the new line character properly. Now I have another new variable say str2. I want to concatenate in this way.. str1 + newline character + and then str2. That's I... (3 Replies)
Discussion started by: dahlia84
3 Replies

8. Shell Programming and Scripting

Matching white space through Grep

Hello All, I am trying to match white space in patterns through - Grep I tried ] & ] but none of them worked. Then I tried Perl extension '\s' and it worked. So just wanted to know if ] & ] are still supported or have they become deprecated. However they have been mentioned in the... (3 Replies)
Discussion started by: paragkalra
3 Replies

9. UNIX for Dummies Questions & Answers

SED with White Space

Dear Members, Suppose i have a variable test which stores a string as below: test='John drives+++++++++a+++++car' now i want to use sed on the above variable and replace + with a white space, so that i get echo $test should give me 'john drives a car' Between... (1 Reply)
Discussion started by: sandeep_1105
1 Replies

10. Shell Programming and Scripting

stripping white space...

Hi All; Having a problem with a file.. the file contains the following data... (a snapshot) 1331F9E9DB7C2BB80EAEDE3A8F043B94,AL7 1DZ,M,50 186FDF93E1303DBA217279EC3671EA91,NG5 1JU,M,24 3783FFAF602015056A8CD21104B1AAAF,CH42 4NQ,M,17 It has 3 columns sepreated by a , the second column... (7 Replies)
Discussion started by: Zak
7 Replies
Login or Register to Ask a Question