網頁

2016年12月30日 星期五

UNIX入門課程1051_ex9

http://erdos.csie.ncnu.edu.tw/~klim/unix-intro/unix-intro-1051.html

作業九: figure out the average for each user(awk programming)
與作業七相同, 但改用 awk 來作. input 的 data 中的 name 可能會重複出現.
$ cat ex9-k1
klim 10 20 milk 199 joe 20
# klim is appeared again!
oak 100 red 200 10 klim 30
joe 199  marry 20 julie 2000  #joe joe
$ ./ex9.awk < ex9-k1
klim = 20.00
milk = 199.00
oak = 100.00
red = 105.00
joe = 109.50
marry = 20.00
julie = 2000.00
$

解答:
#!/usr/bin/awk -f
BEGIN {
}
{
  sub(/#.*/,"")
  if ( $0 != "" ) {
    for (i=1; i<=NF; i++) {
      if ( $i ~ /[a-z]/ ) {  # $1 is name
        name=$i
      }
      else {                 # $1 is number
        array[name]=array[name]+$i
        num_count[name]=num_count[name]+1
      }
    } #for i END
  } #if END
}
END {
for (i in array)
  printf("%s = %.2f\n",i, array[i]/num_count[i])
}
執行結果:


 

2016年12月23日 星期五

UNIX入門課程1051_ex8

http://erdos.csie.ncnu.edu.tw/~klim/unix-intro/unix-intro-1051.html

作業八: figure out the average and the max three (awk programming)
跟作業六一樣, 但改用 awk 來寫.
請全用 awk 來寫, 不要混用 sh 或 sed.
請寫成一個 script file, 並設定成可執行.
input 的 data 內可能有 comment.

$ cat k1
10 20 30 #this is a comment
8 7
# this is also a comment
2
$ ./ex8.awk k1
the max three are: 30 20 10
the average is   : 12.83
$

解答:

#!/usr/bin/awk -f
BEGIN {
}
{
  sub(/#.*/ , "")
  if ( $0 != "" ) {
    for (i=1; i<=NF; i++) {
      num[count]=$i
      sum+=$i
      count++
    } #for END
  } #if END
}
 
END {
  asort(num) #sort num[] array
  printf("the max three are: %d %d %d\n",num[count],num[count-1],num[count-2])
  printf("the average is   : %.2f\n",sum/count)
} 

注意:程式中asort函數在GNU awk 3.1才有提供


執行結果:



2016年12月16日 星期五

UNIX入門課程1051_ex7

http://erdos.csie.ncnu.edu.tw/~klim/unix-intro/unix-intro-1051.html

作業七: figure out the average for each user(sh programming)
$ cat ex7-k1
klim 10 20 30 milk 199 
oak 100 red 200 10
joe 199 20 marry 20 julie 2000
$ ./ex7.sh < ex7-k1
klim = 20.00
milk = 199.00
oak = 100.00
red = 105.00
joe = 109.50
marry = 20.00
julie = 2000.00
$

解答:

#!/bin/bash
function AVERAGE () {
  avg=$(echo "scale=2; $2/$3" | bc)
  echo "$1 = $avg"
}
function DATA () {
  #if [[ $1 != [0-9]*  &&  -n $1 ]]; then
  if [[ $1 =~ [a-z]+  &&  -n $1 ]]; then
    #echo "name:$1"
    name[index]=$1
    let index+=3
    sum=0
    count=0
  else
    let sum=sum+$1
    if [ ! -z $1 ]; then
      let count++
    fi
      name[index-2]=$sum
      name[index-1]=$count
  fi 
}
index=1
while read line
do
  if [ "$line" != "" ]; then
    set $(echo $line)
    field=$#    #*** $# is field count
    for ((i=1; i<=field; i++))
    do
      #echo "data:$1"
      DATA $1
      shift
    done
  fi
done #while END
for ((i=1; i<=index-3; i=i+3))
do
  #echo "Name:${name[$i]}  sum=${name[i+1]}  count=${name[i+2]}"
  AVERAGE ${name[$i]} ${name[i+1]} ${name[i+2]}
done

執行結果:




2016年12月6日 星期二

UNIX入門課程1051_ex6

http://erdos.csie.ncnu.edu.tw/~klim/unix-intro/unix-intro-1051.html

作業六: figure out the average and the max three (sh programming)
寫一個 sh 的 program, 算出所有數字的平均及最大的三個數
數字從檔案讀入(若有給檔名的話), 或是從 keyboard 讀入(即 stdin, 若沒給檔名的話).
平均數要算到小數第二位.
若數字的個數小於三個, 則補 0.

$ cat k1
10 20 30
8 7
2
$ ./ex6.sh k1
the max three are: 30 20 10
the average is   : 12.83
$ cat k2
888 2
30
11 222
$ ./ex6.sh k1 k2
the max three are: 888 222 30
the average is   : 111.81
$ ./ex6.sh
1 2 3
4
5 6
the max three are: 6 5 4
the average is   : 3.50
$


解答:
#!/bin/bash
#calc average function
function AVERAGE () {
    avg=$(echo "scale=2; $1/$2" | bc )
    #echo "the average is   : $avg"
}
#bubble sort function
function SORT() {
    for ((i=0; i<count; i++))
    do
        for ((j=i; j<count; j++))
        do
            if [ ${data[j]} -gt ${data[i]} ]; then
                let temp=data[j]
                let data[j]=data[i]
                let data[i]=temp
            fi
        done
    done
}

if [ -z $1 ]; then  #manual input
  while read line
  do
    if [ "$line" != "" ]; then
      #set $(echo $line | tr : ' ')
      set $(echo $line)
      field=$#
      for ((i=1; i<=field; i++))
      do
        data[count]=$1
        let sum+=$1
        let count+=1
        shift
      done
    else
      break
    fi
  done<&0  
else  #file(s) input
  total_file=$#
  for ((i=1; i<=$total_file; i++)) #file name put in f[] array
  do
    f[i]=$1
    shift
  done
  for ((j=1; j<=$total_file; j++))
  do
    exec<"${f[j]}"
    while read line
    do
      set $(echo $line)
      field=$#
      for ((i=1; i<=field; i++))
      do
        data[count]=$1
        let sum+=$1
        let count+=1
        shift
      done
    done
  done #for END
fi
SORT #Run Bubble sort function
echo "the max three are: ${data[0]} ${data[1]} ${data[2]}"
AVERAGE $sum $count #Run calc average function
echo "the average is   : $avg"
執行結果: