KSH
HP-SOL-Lin
Cannot use xAWK
I have several strings that are quite long and i want to break them down into smaller substrings.
What I have
String = "word1 word2 word3 word4 .....wordx"
What I want
String1="word1 word2"
String2="word3 word4"
String3="word4 word5"
Stringx="wordx wordx+1"
etc.....
How Can i break this up to where if my string is longer than x words, break into smaller strings no longer than x? I have no idea how long each string will be. We can test for it, but it will not be consistent.
StrLen=`echo $string |wc -w`
Some strings are longer than 2000 words, so i cannot use a shell array as there is a max of 1024 fields.
ideas?
Here is what I have come up with based on comments below
FIELDS=`echo $String | wc -w`
((n=$FIELDS/2+1))
i=1
while [[ $i -le $n ]]; do
typeset STRING$i=`echo $String | cut -d" " -f$CUTSTART-$CUTEND`
do stuff
i=`expr $i+1`
CUTSTART=`expr $CUTSTART+1`
CUTEND=`expr $CUTEND+1`
done
Still seem to be having issues with the typeset peice.
Assumptions
i=1
CUTSTART=1
CUTEND=2
String=one two three
myserver> typeset STRING=`echo $String | cut -d" " -f$CUTSTART-$CUTEND`
myserver> echo $STRING
myserver> one two
myserver>
myserver> typeset STRING$i=`echo $String | cut -d" " -f$CUTSTART-$CUTEND`
myserver> echo $STRING1
myserver> one
what is this issue with $i messing up my echo|cut command?
Here’s a loop that uses
readto extract two words at a time: