我將原始日志片段作為自定義命令的輸出轉儲到控制臺上:
bash$ custom-command
current-capacity: 3%, buffer: 1024, not-used/total: 10/10, IsEnabled: 0. Up since Thu Jun 23 11:54:14 2022
current-capacity: 0%, buffer: 1024, not-used/total: 25/25, IsEnabled: 0. Up since Thu Jun 23 11:54:14 2022
current-capacity: 0%, buffer: 1024, not-used/total: 15/15, IsEnabled: 1. Up since Thu Jun 23 11:54:14 2022
我需要像下面這樣的 CSV 格式來根據某些標準實時捕獲狀態,然后我可以在加載到 SQL 資料庫之前定期將輸出重定向到 CSV 檔案。
current-capacity, buffer, not-used/total, IsEnabled, Up since
3%, 1024, 10/10, 0, Thu Jun 23 11:54:14 2022
0%, 1024, 25/, 0, Thu Jun 23 11:54:14 2022
0%, 1024, 10/10, 0, Thu Jun 23 11:54:14 2022
我已經嘗試過 AWK 但仍然面臨問題,因為它大部分都是逗號分隔的,除了IsEnabled: 0以. 有辦法嗎?我對awk很陌生。.Uptime
uj5u.com熱心網友回復:
awk -F' :|, |: ' -v OFS="," '
NR==1{
print $1,$3,$5,$7,"Up since"
}
{
print $2,$4,$6,gensub(/([0-1])\. ?Up since ?(.*)/, "\\1,\\2", 1, $8)
}' file
current-capacity,buffer,not-used/total,IsEnabled,Up since
3%,1024,10/10,0,Thu Jun 23 11:54:14 2022
0%,1024,25/25,0,Thu Jun 23 11:54:14 2022
0%,1024,15/15,1,Thu Jun 23 11:54:14 2022
awk -F' :|, |: ' '
function formatRow(r){
return gensub(/([0-1])\. ?(Up since)/, "\\1, \\2:", 1, r)
}
NR==1{
$0 = formatRow($0)
for(i=1;i<=NF-1;i =2) printf (i==NF-1 ? "%s\n" : "%s,"), $i
}
{
$0 = formatRow($0)
for(i=2;i<=NF;i =2) printf (i==NF ? "%s\n" : "%s,"), $i
}' file
current-capacity,buffer,not-used/total,IsEnabled,Up since
3%,1024,10/10,0,Thu Jun 23 11:54:14 2022
0%,1024,25/25,0,Thu Jun 23 11:54:14 2022
0%,1024,15/15,1,Thu Jun 23 11:54:14 2022
uj5u.com熱心網友回復:
它只是撰寫一個匹配輸出的正則運算式并對其進行轉換。
sed -E 's/current-capacity: (.*)%, buffer: (.*), not-used/total: (.*), IsEnabled: (.*). Up since (.*)/\1%,\2,\3,\4/'
uj5u.com熱心網友回復:
在每個 Unix 機器上的任何 shell 中使用任何 awk:
$ cat tst.awk
BEGIN { FS="[:,] "; OFS=", " }
match($0,/\. [^ ] [^ ] /) {
$0 = substr($0,1,RSTART-1) "," substr($0,RSTART 1,RLENGTH-1) ":" substr($0,RSTART RLENGTH)
}
NR == 1 {
for ( i=1; i<NF; i =2 ) {
printf "%s%s", $i, (i<(NF-1) ? OFS : ORS)
}
}
{
for ( i=2; i<=NF; i =2 ) {
printf "%s%s", $i, (i<NF ? OFS : ORS)
}
}
$ awk -f tst.awk file
current-capacity, buffer, not-used/total, IsEnabled, Up since
3%, 1024, 10/10, 0, Thu Jun 23 11:54:14 2022
0%, 1024, 25/25, 0, Thu Jun 23 11:54:14 2022
0%, 1024, 15/15, 1, Thu Jun 23 11:54:14 2022
在上面的第一步是使用match() { ... }使. Up since Thu每個輸入行末尾的欄位使用與輸入的其余部分相同的,和:分隔符,, Up since: Thu以便決議現在一致的輸入的其余代碼很容易。
uj5u.com熱心網友回復:
歡迎來到 StackOverflow。感謝您提供示例資料和所需的輸出。建議您在此處學習markdown 格式語法,因為您的代碼是作為參考的 HTML 輸入的。最好使用代碼標簽。這將輸出固定寬度的文本,并且更易于閱讀。
至于您的問題,您可以使用match陳述句 ingawk使用正則運算式捕獲所有欄位,因為您的輸入資料的格式相同。
像這樣的東西會做需要的:
BEGIN{
# set output separator to comma space
OFS=", "
# define the regular expression to capture needed
# See https://regex101.com/r/K4wYoB/1
#
# ([^,]) captures all until next comma, not including comma
# (.) captures single character
# (.*) at the end, captures remaining
#
# did not use full words, since it was not needed.
#
myregexp="y: ([^,]*).*r: ([^,]*).*al: ([^,]*).*led: (.).*ce (.*)"
# print header for output
print "current-capacity, buffer, not-used/total, IsEnabled, Up since"
}
# loop lines. Skipping header line
NR>1{
# capture data fields
match($0, myregexp, a)
# print the line from "a" array
print a[1], a[2], a[3], a[4], a[5]
}
uj5u.com熱心網友回復:
輸入格式,是米勒輸入格式之一
如果你簡單地運行
mlr --ocsv --ips : clean-whitespace input.txt
你將會有
current-capacity,buffer,not-used/total,IsEnabled
3%,1024,10/10,0. Up since Thu Jun 23 11:54:14 2022
0%,1024,25/25,0. Up since Thu Jun 23 11:54:14 2022
0%,1024,15/15,1. Up since Thu Jun 23 11:54:14 2022
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/495898.html
上一篇:找不到和讀取.csv檔案
