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

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 :-)

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.

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.

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

the last one does pad, but it doesn't stop. I'm going to see why. Thanks for all your help

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)?

I copied it character for character, then had someone else proof-read it. Unless I hit Ctl-C it keeps on adding spaces.

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 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

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

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.

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 ]

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 ' ?

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.

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

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$

you could also check to see what is happening in the file with an some echo statments

#!/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
    # this echo statmernt may show you what is happening
    echo `wc -m $file`
    # thes next two lines of code will put a pause inbetween each loop so you are able to read it as it happens
    echo "enter return to continue"
    read X
done

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$

hmm. it works on mine. I am guessing you are not able to cut and paste. Maybe you have a typo.

I'm still getting a too many arguments error. I can try this script at home on Linux. Maybe it's not working because it's on Solaris 9 right now.

I have an idea. Maybe on solaris the output of wc -m is different. on linux the output of

wc -m file_name

is

23 file_name

this is assumbing there is 23 characters.

what does the output of wc -m look like with solaris?l

it is the same. I thought that there was a command line in awk or sed that gives you the ability to add a specified amout of spaces to a file and send it to the stdout. I just can't find it.

DUDE!!! It works!!! I had to just add -d to the tr and then WHAM!
Thanks sooo much for all your help. I owe ya one.

I was thinking about that script, using the tr command to splice off the letters will fail on any file name that contain numbers. The following command will fail becaise it will also remove numbers from your charcater count

wc -m $FILE | tr -d $FILE

the problem is if the file name has a number in it and the amount of characters in that file has that number also, it will remove the number.
lets say the output of the command

wc -m test_file2.avi

you will get

2445 test_file2.avi

now tr will remove all of the following characters "test_file2.avi" so your trimmed output will be missing a 2 like this

445

you could solve this problem by using sed(I know it was not working for you). Using the substitiuion parameter of sed would allow you to find the whole string and substitute it with nothing, in effect deleting it.

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 | sed "s/$FILE//"` -lt $TOTAL ]
    do echo "" >> $FILE
done

Great thanks, your right. Plus I had to tweak it some more because I was told sometimes the file has too many spaces at the end so the script had to add OR subtract spaces. This wasn't too bad though, I just added a sed command and mv after the file name was read:

sed -e '/^$/ d' $FILE>temp_padding_file
mv temp_padding_file $FILE

and that stripped the spaces off end before even starting to loop. Thanks again for your help.

This article has been dead for over six months. Start a new discussion instead.