RSS Forums RSS
Please support our Shell Scripting advertiser: Programming Forums
Views: 12664 | Replies: 27
Reply
Join Date: Jul 2005
Posts: 23
Reputation: gritty is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
gritty gritty is offline Offline
Newbie Poster

help with shell script padding files with spaces

  #1  
Jul 20th, 2005
I have a script that I'm working on to pad files with spaces. The spaces need will vary. This is what I have so far but it isn't working. Any suggestions?

#!/bin/bash
echo -n "Enter filename "
read filename
echo -n "Enter start size "
read start
echo -n "Enter stop size "
read stop
while [ $start < $stop ]; do
sed '/$/G' >>$filename
done
wc -m $filename
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: help with shell script padding files with spaces

  #2  
Jul 21st, 2005
I am not totally following what the whole script is supposed to be doing. By padding, do you mean addings spaces, if so, what are you adding the spaces to? I have only used sed alittle bit, but I am not following what you are trying to accomplish with the sed command. I would like to help just need a little more info :-)
Reply With Quote  
Join Date: Jul 2005
Posts: 23
Reputation: gritty is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
gritty gritty is offline Offline
Newbie Poster

Re: help with shell script padding files with spaces

  #3  
Jul 21st, 2005
Well, I need to add blank spaces at the end of a UNIX/Linux file so that the user can specify what length it needs to be and it will be filled in the rest of the way with spaces so it will satisfy the required characters. When the user asked me about this it sounded a lot more complicated than it now seems. I believe all I need to do is have him put an awk/sed command (whichever can do this) with the number of spaces he needs and let it run from a command line instead of creating a script. I hope this helps. The request was vague and now I'm going on three days trying to punch through this.
Reply With Quote  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: help with shell script padding files with spaces

  #4  
Jul 21st, 2005
Something like this should work for you
echo "enter the file name you want to pad"
read file
echo "enter the amount of spaces you want to add"
read add
x=0
while [ $x != $add ]
    do
    x=`echo "$x + 1" | bc`
    echo "" >> file
    done
this will add the amount of space you need. Are you looking to have the script do more?

edit added later//

I just reread your post, now I see what you are trying to do, I will see if I can do it differently.
Reply With Quote  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: help with shell script padding files with spaces

  #5  
Jul 21st, 2005
this should work, it seems messy, but seems to do the job
#!/bin/bash

echo "enter the name of the file you want to pad"
read file
echo "enter the total amount of characters you need in the file"
read total
while [ "`wc -m $file`" != "`echo "$total $file"`" ]
    do echo "" >> $file
done
Reply With Quote  
Join Date: Jul 2005
Posts: 23
Reputation: gritty is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
gritty gritty is offline Offline
Newbie Poster

Re: help with shell script padding files with spaces

  #6  
Jul 21st, 2005
the last one does pad, but it doesn't stop. I'm going to see why. Thanks for all your help
Reply With Quote  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: help with shell script padding files with spaces

  #7  
Jul 21st, 2005
what do you mean it does not stop? It seemed to work when I tested it.
Reply With Quote  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: help with shell script padding files with spaces

  #8  
Jul 21st, 2005
Originally Posted by gritty
the last one does pad, but it doesn't stop. I'm going to see why. Thanks for all your help

you give the script the variable, which is the file you want to pad

then you tell it how many characters you need in the file, then it fills the file with spaces until it reaches the amount you told it to pad to. Then it stops.

How do you want it to work differently, just curious(having fun)?
Reply With Quote  
Join Date: Jul 2005
Posts: 23
Reputation: gritty is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
gritty gritty is offline Offline
Newbie Poster

Re: help with shell script padding files with spaces

  #9  
Jul 21st, 2005
I copied it character for character, then had someone else proof-read it. Unless I hit Ctl-C it keeps on adding spaces.
Reply With Quote  
Join Date: Jul 2005
Posts: 23
Reputation: gritty is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
gritty gritty is offline Offline
Newbie Poster

Re: help with shell script padding files with spaces

  #10  
Jul 21st, 2005
I just figured it out. The wc -m command also adds the filename (at least in the UNIX/Linux version I've tried it on) so I need to pipe it to awk and strip the filename away so I have just the number. I think that was the only issue. I'll post my findings
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:12 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC