Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Missing Spaces? 1

Status
Not open for further replies.

iaresean

Programmer
Mar 24, 2003
570
ZA
Hi all;

I am trying to read a file in to test if the string "User" is present if so I would like to place a '#' infront of "User". The script I have below seems to do the job, except the spaces/indents of the original file go missing.

Here is an example of the file I am reading:
Code:
/tmp/sally.txt
   Hello there Sally
      When is it going to happen
   User
     Today perhaps?
       User
[code]
Here is the code I use to change the file:
[code]
#!/bin/sh
                                                                                                                 
infile="/tmp/sally.txt"
                                                                                                                 
while read line ; do
        t=${line/User/\#User}
        echo ${line} >> /tmp/other.txt
done < $infile

mv /tmp/other.txt /tmp/sally.txt
And here is the copy of the final output:
Code:
/tmp/sally.txt
Hello there Sally
When is it going to happen
#User
Today perhaps?
#User

As you can see the "neat" indents have dissappeared, any ideas on how to prevent this?

Any and all help is greatly appreciated!

Sean. [peace]
 
Try something like this:
cp $infile $infile.$$
sed -e 's!User!#User!g' $infile.$$ >$infile && rm $infile.$$

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Sorry about the mess above I didn't slash the one code box, here this is more readable:

Here is the file I want to change:
Code:
/tmp/sally.txt
   Hello there Sally
      When is it going to happen
   User
     Today perhaps?
       User
Here is the code I use to change the file:
Code:
#!/bin/sh

infile="/tmp/sally.txt"

while read line ; do
        t=${line/User/\#User}
        echo ${t} >> /tmp/other.txt
done < $infile

mv /tmp/other.txt /tmp/sally.txt
And here is the copy of the final output:
Code:
/tmp/sally.txt
Hello there Sally
When is it going to happen
#User
Today perhaps?
#User

Sorry about that, thanks;

Sean. [peace]
 
Wow, super fast response PHV!!

You definitely deserve a star!

Thanks. :)

Sean. [peace]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top