我有一個看起來像這樣許多數據文件: ,8/9/2015 Timezone,-6 , Serial No.,19000000395CCE41 Location:,LS_trap_9u High temperature limit (�C),20.12 Low temperature limit (�C),0.05 Date - Time,Temperature (�C) 5/28/2015 6:00,20 5/28/2015 8:00,22.6 5/28/2015 10:00,27.1 5/28/2015 12:00,26.1 5/28/2015 14:00,27.1 5/28/2015 16:00,26.1 5/28/2015 18:00,24.6 5/28/2015 20:00,23.6 5/28/2015 22:00,22.6 5/29/2015 0:00,22.1
我分析這個腳本這些文件:如何從awk中提取特定字符串的最終字符並將其附加到列?
awk -vFS=, -vOFS=, \
'{gsub("\"","")}
FNR==4{s=$2}
FNR==5{l=$2}
FNR>8{gsub(" ",OFS);print l,s,FILENAME,$0}' \
*.csv > formatted_log.csv
printf "\nDone\n"
我想從「祿」字符串中提取的最後一個字符(在這種情況下,「U 「)並將其附加到另一列。
最後的文件應該是這個樣子:
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,u,5/28/2015,5:59,20.1
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,u,5/28/2015,7:59,27.6
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,u,5/28/2015,9:59,30.1
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,u,5/28/2015,11:59,29.6
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,u,5/28/2015,13:59,29.6
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,u,5/28/2015,15:59,28.1
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,u,5/28/2015,17:59,26.1
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,u,5/28/2015,19:59,23.6
我嘗試迄今爲止是這樣的:
awk -vFS=, -vOFS=, \
'{gsub("\"","")}
FNR==4{ser=$2}
FNR==5{loc=$2}
FNR>8{gsub(" ",OFS);print loc,ser,FILENAME,${loc:(-1)},$0}' \
*.csv > formatted_log.csv
我收到以下錯誤:
awk: cmd. line:4: FNR>8{gsub(" ",OFS);print loc,ser,FILENAME,${loc:(-1)},$0}
awk: cmd. line:4: ^syntax error
awk: cmd. line:4: FNR>8{gsub(" ",OFS);print loc,ser,FILENAME,${loc:(-1)},$0}
awk: cmd. line:4: ^syntax error
awk: cmd. line:4: FNR>8{gsub(" ",OFS);print loc,ser,FILENAME,${loc:(-1)},$0}
awk: cmd. line:4: ^syntax error
更改對此的腳本:
awk -vFS=, -vOFS=, \
awk -vFS=, -vOFS=, \
'{gsub("\"","")}
FNR==4{ser=$2}
FNR==5{loc=$2}
my_loc="${loc:(-1)}"
FNR>8{gsub(" ",OFS);print loc,ser,FILENAME,my_loc,$0}' \
*.csv > formatted_log.CSV
printf "\nDone1\n"
awk -vFS=, -vOFS=, \
'{gsub("\"","")}
FNR==4{ser=$2}
FNR==5{loc=$2}
my_loc="${loc:(-1)}"
FNR>8{gsub(" ",OFS);print loc,ser,FILENAME,my_loc,$0}' \
*.csv > formatted_log.CSV
printf "\nDone1\n"
將不需要的額外行添加到formattted_log.csv文件。看起來像這樣:
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,5/28/2015,5:59,20.1
5/28/2015 7:59,27.6
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,5/28/2015,7:59,27.6
5/28/2015 9:59,30.1
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,5/28/2015,9:59,30.1
5/28/2015 11:59,29.6
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,5/28/2015,11:59,29.6
5/28/2015 13:59,29.6
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,5/28/2015,13:59,29.6
5/28/2015 15:59,28.1
如何從awk中提取特定字符串的最終字符?
' '/ ^位置:/ {代碼= SUBSTR($ 0,長度($ 0))} ...''' – karakfa