敏捷、分布式、ALM過(guò)程自動(dòng)化、企業(yè)應(yīng)用架構(gòu)
          posts - 14, comments - 0, trackbacks - 0, articles - 1
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          日歷

          <2012年3月>
          26272829123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          公告

          本博客內(nèi)容均為原創(chuàng)。
          有問(wèn)題可聯(lián)系郵箱:matuobasyouca@gmail.com

          文章分類(lèi)(1)

          文章檔案(1)

          搜索

          •  

          最新評(píng)論

          Example 2-2. A program for finding the maximum recorded temperature by year from NCDC weather records

          #!/usr/bin/env bash

          for year in all/*

          do

            echo -ne `basename $year .gz`"\t"

            gunzip -c $year | \

              awk '{ temp = substr($0, 88, 5) + 0;

                     q = substr($0, 93, 1);

                     if (temp !=9999 && q ~ /[01459]/ && temp > max) max = temp }

                   END { print max }'

          done

           

          使用linux腳本打印每年最高溫度,先解釋一下該腳本幾個(gè)注意點(diǎn)。

          腳本目的是發(fā)現(xiàn)每年的最高溫度,第一句for year in 后的all/*表示在名稱為all的文件夾下每年度的溫度信息都以 1990.gz 方式存在。使用gunzip方式解壓并打印,對(duì)打印的內(nèi)容使用awk函數(shù)進(jìn)行處理,獲取最大溫度,單個(gè)文件處理完畢后打印max

           

          在上一篇中獲取的數(shù)據(jù)包是這樣,年度為文件夾,當(dāng)中包含若干個(gè)溫度詳情文件。

          E:\testData\1990\010010-9999-1990.gz

          E:\testData\1990\010014-9999-1990.gz

          E:\testData\1990\010015-9999-1990.gz

          E:\testData\1990\010016-9999-1990.gz

           

          從后面Appendix C的描述中得知,實(shí)際上作者對(duì)這樣的數(shù)據(jù)進(jìn)行了處理,因?yàn)?/span>hadoop在處理大量的小文件時(shí)無(wú)法達(dá)到很高的效率,因此作者使用hadoop將小文件合并,并且給出了代碼。

           

          我比較希望能夠使用腳本處理,將所有的gz解壓之后,合并成為一個(gè)文件,打包成gz的格式,這樣就能完全符合之前那段腳本的處理方式。所以,腳本如下:

          packyear

          #! /bin/sh

          # /usr/data/packyear

           

          # unzip all gz files in data

          for yeards in data/*

          do

                 # unzip all gz files in year directory

                 for gzfile in $yeards/*

                 do

                        gunzip $gzfile

                 done

                

                 # cat all content to year file

                 cat $yeards/* | head -2 >> $yeards.tc

           

                 # remove year directory

                 rm -rf $yeards

                 mv $yeards.tc $yeards

           

                 # zip the tc file

                 gzip $yeards

          done

           

          根據(jù)實(shí)際路徑改寫(xiě)的計(jì)算最大溫度的腳本

          maxyear

          #! /bin/sh

          # /usr/data/ maxyear

           

          for year in /usr/data/*

          do

            basename $year .gz

            gunzip -c $year | \

              awk '{temp=substr($0, 88, 5)+0;

                  q=substr($0, 93, 1);

                  if(temp !=9999 && q ~ /[01459]/ && temp > max) max = temp}

                END {print max}'

          done

          這個(gè)腳本最終顯示出來(lái)會(huì)是:

          1990

          3

          這樣的格式。由于對(duì)數(shù)據(jù)結(jié)構(gòu)的不熟悉,所以不確定顯示出來(lái)的數(shù)據(jù)是否正確,但是基本的腳本和數(shù)據(jù)操作方式就是這樣了。

          主站蜘蛛池模板: 襄城县| 略阳县| 凉城县| 乌拉特前旗| 万盛区| 察隅县| 湘阴县| 鹰潭市| 沿河| 通榆县| 修水县| 盐边县| 中山市| 曲阳县| 交城县| 曲周县| 明星| 海兴县| 沈阳市| 乌审旗| 永登县| 台东市| 攀枝花市| 吴旗县| 沁阳市| 三穗县| 松溪县| 平潭县| 三台县| 彰化县| 宜阳县| 卢龙县| 九江市| 牡丹江市| 玉树县| 田东县| 阿拉尔市| 海林市| 宾川县| 定襄县| 翼城县|