這篇文章主要介紹“如何用golang處理文本小計(jì)”,在日常操作中,相信很多人在如何用golang處理文本小計(jì)問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何用golang處理文本小計(jì)”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

創(chuàng)新互聯(lián)建站作為成都網(wǎng)站建設(shè)公司,專注重慶網(wǎng)站建設(shè)公司、網(wǎng)站設(shè)計(jì),有關(guān)企業(yè)網(wǎng)站制作方案、改版、費(fèi)用等問題,行業(yè)涉及成都加固等多個領(lǐng)域,已為上千家企業(yè)服務(wù),得到了客戶的尊重與認(rèn)可。
linux 64bit
go version go1.7.4 linux/amd64
physical machine
smartctl 6.2 2013-07-26 r3841 [x86_64-linux-3.10.0-327.36.2.el7.x86_64] (local build)
使用smartctl命令收集物理機(jī)上的硬盤信息。需要的硬盤信息如下:
date: ‘2016-02-09’, string
serial_number: MJ0351YNG9Z0XA, string
model: Hitachi HDS5C3030ALA630, string
capacity_bytes: 3000592982016, string
normalize: 100, int
raw: 32296, string
以上需要的信息都可以通過smartctl配合相關(guān)參數(shù)拿到,但是格式是很原始,需要我們根據(jù)需求抽取出真正需要的信息。
比如normalize和raw是硬盤的屬性信息,可通過smartctl -A device獲取:
? ~ sudo smartctl -A /dev/sda smartctl 6.2 2013-07-26 r3841 [x86_64-linux-3.10.0-327.36.2.el7.x86_64] (local build) Copyright (C) 2002-13, Bruce Allen, Christian Franke, www.smartmontools.org === START OF READ SMART DATA SECTION === SMART Attributes Data Structure revision number: 10 Vendor Specific SMART Attributes with Thresholds: ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE 1 Raw_Read_Error_Rate 0x000f 118 099 006 Pre-fail Always - 199795992 3 Spin_Up_Time 0x0003 097 097 000 Pre-fail Always - 0 4 Start_Stop_Count 0x0032 100 100 020 Old_age Always - 149 5 Reallocated_Sector_Ct 0x0033 100 100 010 Pre-fail Always - 0 7 Seek_Error_Rate 0x000f 077 060 030 Pre-fail Always - 54764573 9 Power_On_Hours 0x0032 089 089 000 Old_age Always - 9792 10 Spin_Retry_Count 0x0013 100 100 097 Pre-fail Always - 0 12 Power_Cycle_Count 0x0032 100 100 020 Old_age Always - 149 183 Runtime_Bad_Block 0x0032 100 100 000 Old_age Always - 0 184 End-to-End_Error 0x0032 100 100 099 Old_age Always - 0 187 Reported_Uncorrect 0x0032 100 100 000 Old_age Always - 0 188 Command_Timeout 0x0032 100 099 000 Old_age Always - 2 2 2 189 High_Fly_Writes 0x003a 100 100 000 Old_age Always - 0 190 Airflow_Temperature_Cel 0x0022 060 055 045 Old_age Always - 40 (Min/Max 26/40) 191 G-Sense_Error_Rate 0x0032 100 100 000 Old_age Always - 0 192 Power-Off_Retract_Count 0x0032 100 100 000 Old_age Always - 25 193 Load_Cycle_Count 0x0032 098 098 000 Old_age Always - 4708 194 Temperature_Celsius 0x0022 040 045 000 Old_age Always - 40 (0 6 0 0 0) 197 Current_Pending_Sector 0x0012 100 100 000 Old_age Always - 0 198 Offline_Uncorrectable 0x0010 100 100 000 Old_age Offline - 0 199 UDMA_CRC_Error_Count 0x003e 200 200 000 Old_age Always - 0 240 Head_Flying_Hours 0x0000 100 253 000 Old_age Offline - 9049h+51m+35.226s 241 Total_LBAs_Written 0x0000 100 253 000 Old_age Offline - 4749106878 242 Total_LBAs_Read 0x0000 100 253 000 Old_age Offline - 126575500815
廠商這類靜態(tài)信息可通過sudo smartctl -i device獲取:
? ~ sudo smartctl -i /dev/sda smartctl 6.2 2013-07-26 r3841 [x86_64-linux-3.10.0-327.36.2.el7.x86_64] (local build) Copyright (C) 2002-13, Bruce Allen, Christian Franke, www.smartmontools.org === START OF INFORMATION SECTION === Model Family: Seagate Barracuda 7200.14 (AF) Device Model: ST1000DM003-1ER162 Serial Number: W4Y3Z28A LU WWN Device Id: 5 000c50 08a2c464e Firmware Version: CC46 User Capacity: 1,000,204,886,016 bytes [1.00 TB] Sector Sizes: 512 bytes logical, 4096 bytes physical Rotation Rate: 7200 rpm Device is: In smartctl database [for details use: -P show] ATA Version is: ACS-2, ACS-3 T13/2161-D revision 3b SATA Version is: SATA 3.1, 6.0 Gb/s (current: 6.0 Gb/s) Local Time is: Tue Feb 7 17:53:34 2017 CST ==> WARNING: A firmware update for this drive is available, see the following Seagate web pages: http://knowledge.seagate.com/articles/en_US/FAQ/207931en http://knowledge.seagate.com/articles/en_US/FAQ/223651en SMART support is: Available - device has SMART capability. SMART support is: Enabled
我們該如何把里面的數(shù)據(jù)抽取出來呢?
正則表達(dá)式
strings(golang stdlib):處理字符串,分割、刪除等
strconv (golang stdlib):處理字符串轉(zhuǎn)換。
os/exec:執(zhí)行smartctl腳本,獲取輸出
對于格式確定的文本,盡量根據(jù)格式過濾掉不需要的數(shù)據(jù),只保留需要的數(shù)據(jù)。
lines := " data1\n data2\n needed1\n needed2\n needed := strings.Split(lines, " ")
對于分割,通常strings.Fields()優(yōu)于strings.Split()。
package mainimport( "strings"
"fmt")func main() {
lines := "194 Temperature_Celsius 0x0022 037 045 000 Old_age Always - 37 (0 6 0 0 0)"
useSplit(lines)
useFields(lines)
}func useSplit(lines string) {
line := strings.Split(lines, " ")
fmt.Println(line)
fmt.Println(len(line))
}func useFields(lines string) {
line := strings.Fields(lines)
fmt.Println(line)
fmt.Println(len(line))
}/*
output
? gist go run main.go
[194 Temperature_Celsius 0x0022 037 045 000 Old_age Always - 37 (0 6 0 0 0)]
42
[194 Temperature_Celsius 0x0022 037 045 000 Old_age Always - 37 (0 6 0 0 0)]
15
*/exec獲取bash script的輸出信息默認(rèn)結(jié)尾是帶一個\n換行符的,要記得處理掉:
package mainimport( "os/exec"
"os"
"fmt")func main() {
devices := getDevices()
fmt.Println(devices)
fmt.Println(len(devices))
}func getDevices() []string {
ret, err := exec.Command("/sbin/smartctl", "--scan").Output() if err != nil {
log.Println("Execute /sbin/smartctl --scan error: %v", err)
os.Exit(-1)
}
linest := strings.Split(string(ret), "\n")
lines := linest[:len(linest)-1] // remove last extra line with output of bash command
var devList = []string{} for _, line := range lines {
device := strings.Fields(line)[0]
devList = append(devList, device)
}
return devList
}/*
remove extra line
[/dev/sda]
1
not remove extra line
[/dev/sda ]
2
*/到此,關(guān)于“如何用golang處理文本小計(jì)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
分享名稱:如何用golang處理文本小計(jì)
網(wǎng)頁網(wǎng)址:http://chinadenli.net/article12/jgpdgc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、用戶體驗(yàn)、軟件開發(fā)、定制網(wǎng)站、網(wǎng)站內(nèi)鏈、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)