Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I need to do some string manipulation to ensure the final way I put together a feed file trailer isn't done with clumsy coding. Anyone out there who knows Unix string manipulation well, please read on...
1. I am assigning an environment variable based on the count of records in a file. As an example:
MY_VAR=`wc -l $MY_FILE | awk '{printf $1}'`
But, as well as containing the number, MY_VAR has to be right-padded with spaces so it is 4 characters in length. e.g. if MY_FILE contains 10 records, MY_VAR must be '10 '. Is there a neat way of doing this?
2. I have a couple of variables containing many repeated instances of the same character. FILLER_1 needs to be 60 instances of '9', and FILLER_2 needs to be 184 spaces.
Currently I have my prototype using assignments like the following example, just to give me something that is up and running:
FILLER_1='999999999999999999999999999999999999999999999999999999999999' # 60 instances of '9'
This is a bit clumsy, so I don't want to include it in the final live system. Does anyone know of a function which can be passed a character and number and output the character the specified number of times?

#!/bin/ksh
#1)
MY_FILE=myfile
MY_VAR=`wc -l < $MY_FILE`
MY_VAR=`echo $MY_VAR ` # change int to string
MY_VAR=`printf "%-4.4s" "$MY_VAR"` # pad with 4 spaces
echo "|$MY_VAR|"#2)
# variation of Eric Pement's center routine at:
# http://www.student.northpark.edu/pemente/sed/sed1line.txt
# STR=9
# echo $STR|sed -e :a -e 's/^.\{1,59\}$/&9/;ta'# paramaterize it:
LEN=60
CHAR=9
LEN=$((LEN-1)) # subtract 1STR=$CHAR
sed_cmd="echo \$STR|sed -e :a -e 's/^.\{1,${LEN}\}$/&${CHAR}/;ta'"
FILLER_1=`eval $sed_cmd`
echo $FILLER_1 # should be 60-9s

Another solution for #1 question:
#!/bin/ksh
VAR=$(echo `wc -l < input.txt`" " | cut -c1-4)
echo "|$VAR|"Luke Chi

I'm sorry.
The # of spaces between the double quotes are four in the following line:
VAR=$(echo `wc -l < input.txt`" " | cut -c1-4)For some reason, it only showed one space when I copied/pasted/posted it. It should be four spaces.
Luke Chi

How to insert 100 chars of 9 in vi?
1. press i - to go to the vi insert mode
2. press 9 - to insert the char 9
3. press Esc - to go to the vi command mode
4. type 99. - to repeat 99 times of the previous vi edit command which was "inserting an i char at the current cursor location"
5. done - your're in the vi command mode now
Luke Chi

The answer to #2 question:
(creating a line with one hundred 9 chars)#!/bin/ksh
typeset -Z100 VAR=0
echo $VAR | tr 0 9
Luke Chi

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |