SH script to parse string and return multi-line file

Hello all, I have been asked to exercise my shell scripting and it has been 10 plus years since I used to do it so I can not remember hardly anything and ask for your help.

What I need to do is copy a line out of a file that can be 10 to 100 characters long, I then need to parse this line into groups of ten and then each group of ten I need to convert them into the binary 1/0s.

So far I have been using the cut command but this is very dirty way of doing it and would like someone to kick start my memory :wall:

#!/bin/sh
K="/tmp/test1"
L="/tmp/test2"
M="/tmp/test3"
N="/tmp/test4"
Z="tmp/binStr"
grep -i -w ${USER1} ${a} | cut -d: -f2 >> $K
grep -i -w ${USER2} ${a} | cut -d: -f2 >> $L
\cp /tmp/TMP_MENU_$$ ${a}
cat $k | cut -c 1-10 >> $M
cat $K | cut -c 11-20 >> $M
cat $K | cut -c 21-30 >> $M
cat $K | cut -c 31-40 >> $M
cat $K | cut -c 41-50 >> $M
cat $K | cut -c 51-60 >> $M
cat $K | cut -c 61-70 >> $M
cat $K | cut -c 71-80 >> $M
cat $K | cut -c 81-90 >> $M
cat $K | cut -c 91-100 >> $M
echo "ibase=16;obase=2;<Please tell me I can use a Variable here?>" | bc >> $Z
#Then the same thing for $L >> $N 

After I figure this out then the fun part happens, I have to do a logical OR against the two files and then convert the binary back to an integer and then put them back into 1 long string, but 1st things first. Any help is drastically appreciated.

this thread might give you a good starting point.

Also, you could replace this:

cat $K | cut -c 1-10 >> $M
cat $K | cut -c 11-20 >> $M
cat $K | cut -c 21-30 >> $M
cat $K | cut -c 31-40 >> $M
cat $K | cut -c 41-50 >> $M
cat $K | cut -c 51-60 >> $M
cat $K | cut -c 61-70 >> $M
cat $K | cut -c 71-80 >> $M
cat $K | cut -c 81-90 >> $M
cat $K | cut -c 91-100 >> $M

with fold -w10 $K > $M

Thanks Chubler_XL that works nicely.

Now on to the logical OR comparisons. To this I have not clue as to where to start :o

Of course there are much better ways to do this than in shell script. An awk/perl script could probably xor two hex values and get the answer directly.

But as a learning excersize in shell string manupliation, looping and logical operations its an intersting problem.

Using bash you could xor two binary strings like this:

#!/bin/bash
A="11101011000101101011"
B="00001111101110100000"
i=0
while [ $i -lt ${#A} ]
do
  [ "${A:i:1}" -eq 1 ] || [ "${B:i:1}" -eq 1 ] && C=${C}1 || C=${C}0
  let i=i+1
done
echo $C