I would like to assign the following values to each column in my file.
if $i is between 1 and -1 (ie -1 < $i < 1) then print A;
if $i is between -2 and -1 && 1 and 2 (ie. -2 < $i < -1 && 1 < $i < 2) then print B;
if $i is between -3 and -2 && 2 and 3 (ie. -3 < $i < -2 && 2 < $i < 3) then print C;
else print D;
I am getting the output, but its printing only A and D; but there are some value which are in the range of B and C. But its not printing B and C, instead its printing D.
#!/usr/bin/env sh
# @(#) s1 Demonstrate if structure and tests.
set -o nounset
echo
## Use local command version for the commands in this demonstration.
set +o nounset
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) awk
set -o nounset
echo
FILE=${1-data1}
echo " Input file $FILE:"
cat $FILE
# Use nawk or /usr/xpg4/bin/awk on Solaris.
echo
echo " Results from awk:"
awk '
# { print "line " NR " is " $0 }
{ for (i = 1; i <= NF; i++) {
# print " Field " i " is " $i
if ($i > -1 && $i < 1) {
printf "A "
}
# else if ($i > -2 && $i < -1 && $i > 1 && $i < 2) {
else if ($i > -2 && $i < 2) {
printf "B "
}
# else if ($i > -3 && $i < -2 && $i > 2 && $i < 3) {
else if ($i > -3 && $i < 3) {
printf "C "
}
else {
printf "D "
}
}
}
{printf "\n"}
' $FILE
exit 0
producing:
% ./s1
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution : Debian GNU/Linux 5.0
GNU bash, version 3.2.39(1)-release (x86_64-pc-linux-gnu)
GNU Awk 3.1.5
Input file data1:
-3
-2
-1
0
1
2
3
Results from awk:
D
C
B
A
B
C
D
Suggestion: think about the original tests for B & C, and follow a value through them.