UNIX/POSIX programming
Friday, November 28, 2008 » programming, uni
Oh, what have I found?! This was my very first program during the university from two thousand five. We had to make an unix program that converts the table made with MS Excel to
stick format and another one that converts back.
Shell programming, UNIX/POSIX. Up to bash!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
case $# in
0) cat >/tmp/temp.txt;
p="/tmp/temp.txt";;
1) if [ "$1" = "-help" ]
then echo "--------------------------------------------------
The program converts the table created in MS Excel
to stick format. Usage: command_file file
--------------------------------------------------"
exit 0
fi
if cd "$1" 2>/dev/null
then
echo "This is a directory!" >&2
exit 1
fi
if cat "$1" 2>/dev/null 1>/dev/null
then 1>/dev/null
p="$1";
else
echo "File does not exist!" >&2
exit 1
fi;;
*) echo Too many parameters! >&2; exit 1;;
esac
l=0
length=`cat "$p" | head -n 1 | tr -d " " | expand | tr -s " " | wc -w`;
for ((i=1;i<=length;i++))
do
a[i]=`cat "$p" | head -n 1 | cut -f$i`;
done
count=`cat "$p" | tail -n +2 | cut -f1 | wc -l`;
cat "$p" | tail -n +2 |
while read
do
let l=l+1;
for ((j=1;j<=length;j++))
do
b=`echo "$REPLY" | cut -f$j`;
echo -n ${a[j]}": "$b
echo
done
if [ $l = $count ]
then echo -n
else echo
fi
done
# -------------------------------- not worked with trap
if [ -r temp.txt ]
then rm /tmp/temp.txt
exit 0
else exit 0
fi
#----------------------------------
|
Converting stick format to MS Excel table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
case $# in
0) cat >/tmp/temp.txt;
p="/tmp/temp.txt";;
1) if [ "$1" = "-help" ]
then echo "--------------------------------------------------
It converts the stick format to table format.
Usage: command_file file
--------------------------------------------------"
exit 0
fi
if cd "$1" 2>/dev/null
then
echo "This is directory!" >&2
exit 1
fi
if cat "$1" 2>/dev/null 1>/dev/null
then 1>/dev/null
p="$1";
else
echo "File does not exist!" >&2
exit 1
fi;;
*) echo Too many paramters! >&2; exit 1;;
esac
len=`cat "$p" | cut -f1 -d ":" | tr "\n" + | sed "s/++/\n/g" | head -n 1 | tr + "\n" | wc -l`
for ((i=1;i<=len;i++))
do
a[i]=`cat "$p" | cut -f1 -d ":" | tr "\n" + | sed "s/++/\n/g" | head -n 1 | cut -f$i -d "+"`;
echo -n ${a[i]}" ";
done
echo
l=0
k=0
check=`cat "$p" | tr "\n" ¤ | sed "s/¤¤/\n/g" | sed "s/¤/\n/g" | sed "s/: /¤/g" | cut -f2 -d "¤" | wc -l`
cat "$p" | tr "\n" ¤ | sed "s/¤¤/\n/g" | sed "s/¤/\n/g" | sed "s/: /¤/g" | cut -f2 -d "¤" |
while read
do
let l=l+1
let k=k+1
b[l]=`echo $REPLY`;
if [ $l = $len ]
then
if [ $k = $check ]
then echo -n ${b[l]}
else echo ${b[l]}
l=0
fi
else echo -n ${b[l]}" "
fi
done
|