User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 392,094 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,851 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser:
Views: 11606 | Replies: 27
Reply
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

  #11  
Jul 21st, 2005
Originally Posted by gritty
I copied it character for character, then had someone else proof-read it. Unless I hit Ctl-C it keeps on adding spaces.

I must be dense, I do not think I am understaning what you are saying(sorry).

I am showing you exactly how it is working on my machine, it seems to stop after it pads the file to the 150 characters I specified
shane@mainbox shane $ echo "some characters"  >file  #this line creates a new file with some characters
shane@mainbox shane $ ./pad  #this runs the script
enter the name of the file you want to pad
file
enter the total amount of characters you need in the file
150
shane@mainbox shane $ wc -m file
150 file
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

  #12  
Jul 21st, 2005
Originally Posted by gritty
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

I tried for a long time to strip the file name off of the output of (wc -m file_name) with either sed or tr , it was not working. you can do the following
wc -m file_name | sed 's/file_name/ /'
this above code will strip off "file_name", but you are not able to use variables in the sed command. For the script to work you need to be able to place a variable in it.

if not for the variable issue, you could also use tr command to strip it
wc -m file_name | tr 'file_name' '  '

for either of those above commands to work, you need to place a variable in place of file_name.

I got frustrated and could not get the file name stripped off of the ouput wc -m, so I went with this method
while [ "`wc -m $file`" != "`echo "$total $file"`" ]
this will only allow it to keep adding characters until the specified amount are added
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

  #13  
Jul 21st, 2005
yeah, I just realized too that it doesn't matter that the filename is in there because you call it again later. I'm not sure why this isn't working on mine though.
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

  #14  
Jul 21st, 2005
do you mind showing me what you have on your script, it would give me a better idea of what you are doing.


edit added later//

I know why I was not able to use variables with sed or tr. The problem was using single quotes. Using double or no quotes works
echo "enter the file name you want to pad"
read FILE
echo "enter the amount of characters you would like in the file"
read TOTAL
while [ `wc -m $FILE | tr $FILE ' '` -lt $TOTAL ]
    do echo "" >> $FILE
done

you could also use sed like this
while [ `wc -m $FILE | sed "s/$FILE/ /"` -lt $TOTAL ]
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

  #15  
Jul 21st, 2005
Originally Posted by gritty
I copied it character for character, then had someone else proof-read it. Unless I hit Ctl-C it keeps on adding spaces.

could you have mixed up this ` with this ' ?
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

  #16  
Jul 21st, 2005
no, I put the "`" in there. I even tried to clear out testfile (the filename I'm testing with) and run it the same way you are but no luck. Here is exactly what I have in the script:
#!/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

I believe it is exactly the same as your.
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

  #17  
Jul 21st, 2005
I erased some nonsence I posted
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

  #18  
Jul 21st, 2005
disreagard what i just posted :o
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

  #19  
Jul 21st, 2005
I think i see the problem. if you choose a number of characters that is less then the file already has, it will run forever. useing this script below should work better
#!/bin/bash

echo "enter the file name you want to pad"
read FILE
echo "enter the amount of characters you would like in the file"
read TOTAL
while [ `wc -m $FILE | tr $FILE ' '` -lt $TOTAL ]
    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

  #20  
Jul 21st, 2005
I'm getting a too many arguments error:

bash-2.05$ testscript2
enter the file name you want to pad
testfile
enter the amount of characters you would like in the file
90
./testscript2: [: too many arguments
bash-2.05$
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)

 

DaniWeb Shell Scripting Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 12:46 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC