如何在文本檔案中分離(獲取)與十六進制數字鍵相關的整行和 DEBUG 的整行,然后存盤在不同的檔案中,其中鍵的格式為:“[uid key]”?即忽略任何不是DEBUG 的行。
在.txt:
[ uid 28fd4583833] DEBUG web.Action
[ uid 39fd5697944] DEBUG test.Action
[ uid 56866969445] DEBUG test2.Action
[ uid 76696944556] INFO test4.Action
[ uid 39fd5697944] DEBUG test7.Action
[ uid 85483e10256] DEBUG testing.Action
輸出檔案命名為 "out" i ".txt",其中 i = 1, 2, 3, 4。即
out1.txt:
[ uid 28fd4583833] DEBUG web.Action
out2.txt:
[ uid 39fd5697944] DEBUG test.Action
[ uid 39fd5697944] DEBUG test7.Action
out3.txt:
[ uid 56866969445] DEBUG test2.Action
out4.txt:
[ uid 85483e10256] DEBUG testing.Action
我試過:
awk 'match($0, /uid ([^]] )/, a) && /DEBUG/ {print > (a[1] ".txt")}' in.txt
uj5u.com熱心網友回復:
如果您愿意更改輸出檔案名以包含鍵(坦率地說,這似乎比名稱中的單計數器更有用),您可以執行以下操作:
awk '/DEBUG/{print > ("out-" $3 ".txt")}' FS='[][ ]*' in.txt
這會將所有DEBUG與帶有 key的字串匹配的行85483e10256放入檔案out-85483e10256.txt等中。
如果您確實想保留一次性柜臺,您可以這樣做:
awk '/DEBUG/{if( ! a[$3] ) a[$3] = counter;
print > ("out" a[$3] ".txt")}' FS='[][ ]*' in.txt
基本上,這個想法是使用正則運算式[][ ]*作為欄位分隔符,它匹配一串方括號或空格。這樣,$1在 initial 之前的 text[是$2string uid,并且$3是 key。這將(應該!)正確獲取可能具有略微不同空白的行的密鑰。我們使用關聯陣列來跟蹤已經看到哪些鍵來跟蹤計數器。但是在輸出檔案名中使用鍵確實更干凈。
uj5u.com熱心網友回復:
如果您的檔案格式與您顯示的一致,您可以這樣做:
awk '
$4!="DEBUG" { next }
!f[$3] { f[$3]= i }
{ print > ("out" f[$3] ".txt") }
' in.txt
uj5u.com熱心網友回復:
第一個解決方案:使用 GNUawk嘗試遵循單個awk代碼。我在哪里使用PROCINFO["sorted_in"]GNU 方法awk。
awk '
BEGIN{
PROCINFO["sorted_in"] = "@ind_num_asc"
}
!/DEBUG/{ next }
match($0,/uid [a-zA-Z0-9] /){
ind=substr($0,RSTART,RLENGTH)
arr[ind]=(arr[ind]?arr[ind] ORS:"") $0
}
END{
for(i in arr){
outputFile=("out" count".txt")
print arr[i] > (outputFile)
close(outputFile)
}
}
' Input_file
第二種解決方案:使用任何awk顯示的樣品,請嘗試以下解決方案。在此處使用您的實際檔案名更改Input_file名稱。
awk '
!/DEBUG/{ next }
match($0,/uid [0-9a-zA-Z] /){
print substr($0,RSTART,RLENGTH)";"$0
}' Input_file |
sort -k2n |
cut -d';' -f2- |
awk '
match($0,/uid [0-9a-zA-Z] /){
if(prev!=substr($0,RSTART,RLENGTH)){
count
close(outputFile)
}
outputFile="out"count".txt"
print > (outputFile)
prev=substr($0,RSTART,RLENGTH)
}
'
第一個解決方案的說明:為第一個解決方案添加詳細說明:
awk ' ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section from here.
PROCINFO["sorted_in"] = "@ind_num_asc" ##Setting PROCINFO["sorted_in"] to @ind_num_asc to sort any array with index.
}
!/DEBUG/{ next } ##If a line does not contain DEBUG then jump to next line.
match($0,/uid [a-zA-Z0-9] /){ ##using match function to match uid space and alphanumeric values here.
ind=substr($0,RSTART,RLENGTH) ##Creating ind which contains sub string of matched sub string in match function.
arr[ind]=(arr[ind]?arr[ind] ORS:"") $0 ##Creating array arr with index of ind and keep adding current line value to same index.
}
END{ ##Starting END block of this program from here.
for(i in arr){ ##Traversing through array arr here.
outputFile=("out" count".txt") ##Creating output file name here as per OP requirement.
print arr[i] > (outputFile) ##printing current array element into outputFile variable.
close(outputFile) ##Closing output file in backend to avoid too many files opened error.
}
}
' Input_file ##Mentioning Input_file name here.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/477519.html
上一篇:訪問多個檔案中嵌套字典中的值?
下一篇:用C程式列印檔案中的所有短語
