我有一個看起來像這樣的檔案(file.txt)
AYOnanl3knsgv2StRr44 CRITICAL","component MMP-FileService [email protected] CODE_SMELL
AYOnanl3knsgv2StRr45 CRITICAL","component MMP-FileService [email protected] CODE_SMELL
AYOnanl3knsgv2StRr46 CRITICAL","component MMP-FileService [email protected] CODE_SMELL
AYOnanl3knsgv2StRr47 CRITICAL","component MMP-FileService [email protected] CODE_SMELL
AYOnanmeknsgv2StRr48 MAJOR", MMP-FileService [email protected] CODE_SMELL
AYOnanm-knsgv2StRr4- BLOCKER", MMP-FileService [email protected] CODE_SMELL
AYOnanm6knsgv2StRr49 MAJOR", MMP-FileService [email protected] CODE_SMELL
AYOnannKknsgv2StRr4_ BLOCKER", MMP-FileService [email protected] BUG
AYODwmuBknsgv2StRqkr MINOR", MMP-FileService [email protected] CODE_SMELL
AYODwmuBknsgv2StRqkt MINOR", MMP-FileService [email protected] CODE_SMELL
AYODwmuBknsgv2StRqks MINOR", MMP-component [email protected] CODE_SMELL
AYODwmuBknsgv2StRqku MINOR", MMP-component [email protected] CODE_SMELL
AYODsI7Fknsgv2StRqac MAJOR", MMP-component [email protected] CODE_SMELL
AYODsI7Nknsgv2StRqad MAJOR", MMP-component [email protected] CODE_SMELL
AYODsI-Qknsgv2StRqai MAJOR", MMP-component [email protected] CODE_SMELL
我必須洗掉第二列中不需要的
","component字符",
然后預期的輸出
AYOnanl3knsgv2StRr44 CRITICAL MMP-FileService [email protected] CODE_SMELL
AYOnanl3knsgv2StRr45 CRITICAL MMP-FileService [email protected] CODE_SMELL
AYOnanl3knsgv2StRr46 CRITICAL MMP-FileService [email protected] CODE_SMELL
AYOnanl3knsgv2StRr47 CRITICAL MMP-FileService [email protected] CODE_SMELL
AYOnanmeknsgv2StRr48 MAJOR MMP-FileService [email protected] CODE_SMELL
AYOnanm-knsgv2StRr4- BLOCKER MMP-FileService [email protected] CODE_SMELL
AYOnanm6knsgv2StRr49 MAJOR MMP-FileService [email protected] CODE_SMELL
AYOnannKknsgv2StRr4_ BLOCKER MMP-FileService [email protected] BUG
AYODwmuBknsgv2StRqkr MINOR MMP-FileService [email protected] CODE_SMELL
AYODwmuBknsgv2StRqkt MINOR MMP-FileService [email protected] CODE_SMELL
AYODwmuBknsgv2StRqks MINOR MMP-component [email protected] CODE_SMELL
AYODwmuBknsgv2StRqku MINOR MMP-component [email protected] CODE_SMELL
AYODsI7Fknsgv2StRqac MAJOR MMP-component [email protected] CODE_SMELL
AYODsI7Nknsgv2StRqad MAJOR MMP-component [email protected] CODE_SMELL
AYODsI-Qknsgv2StRqai MAJOR MMP-component [email protected] CODE_SMELL
這是我嘗試過的
cat file.txt | tr -d '",' | sed 's/component//'
然后輸出我得到
YOnanl3knsgv2StRr44 CRITICAL MMP-FileService [email protected] CODE_SMELL
AYOnanl3knsgv2StRr45 CRITICAL MMP-FileService [email protected] CODE_SMELL
AYOnanl3knsgv2StRr46 CRITICAL MMP-FileService [email protected] CODE_SMELL
AYOnanl3knsgv2StRr47 CRITICAL MMP-FileService [email protected] CODE_SMELL
AYOnanmeknsgv2StRr48 MAJOR MMP-FileService [email protected] CODE_SMELL
AYOnanm-knsgv2StRr4- BLOCKER MMP-FileService [email protected] CODE_SMELL
AYOnanm6knsgv2StRr49 MAJOR MMP-FileService [email protected] CODE_SMELL
AYOnannKknsgv2StRr4_ BLOCKER MMP-FileService [email protected] BUG
AYODwmuBknsgv2StRqkr MINOR MMP-FileService [email protected] CODE_SMELL
AYODwmuBknsgv2StRqkt MINOR MMP-FileService [email protected] CODE_SMELL
AYODwmuBknsgv2StRqks MINOR MMP- [email protected] CODE_SMELL
AYODwmuBknsgv2StRqku MINOR MMP- [email protected] CODE_SMELL
AYODsI7Fknsgv2StRqac MAJOR MMP- [email protected] CODE_SMELL
AYODsI7Nknsgv2StRqad MAJOR MMP- [email protected] CODE_SMELL
AYODsI-Qknsgv2StRqai MAJOR MMP- [email protected] CODE_SMELL
我執行的 shell 命令也適用于其他列(在這種情況下,它也適用于第 3 列),這就是我遇到的問題。有沒有辦法只對第二列應用命令?
有人可以幫我解決這個問題嗎?提前致謝!
注意:我不允許使用 jq 或其他腳本語言,如 JavaScript、Python 等。
uj5u.com熱心網友回復:
它可以一次性完成sub:
awk '{sub(/"[^[:blank:]]*$/, "", $2)} 1' file | column -t
AYOnanl3knsgv2StRr44 CRITICAL MMP-FileService [email protected] CODE_SMELL
AYOnanl3knsgv2StRr45 CRITICAL MMP-FileService [email protected] CODE_SMELL
AYOnanl3knsgv2StRr46 CRITICAL MMP-FileService [email protected] CODE_SMELL
AYOnanl3knsgv2StRr47 CRITICAL MMP-FileService [email protected] CODE_SMELL
AYOnanmeknsgv2StRr48 MAJOR MMP-FileService [email protected] CODE_SMELL
AYOnanm-knsgv2StRr4- BLOCKER MMP-FileService [email protected] CODE_SMELL
AYOnanm6knsgv2StRr49 MAJOR MMP-FileService [email protected] CODE_SMELL
AYOnannKknsgv2StRr4_ BLOCKER MMP-FileService [email protected] BUG
AYODwmuBknsgv2StRqkr MINOR MMP-FileService [email protected] CODE_SMELL
AYODwmuBknsgv2StRqkt MINOR MMP-FileService [email protected] CODE_SMELL
AYODwmuBknsgv2StRqks MINOR MMP-component [email protected] CODE_SMELL
AYODwmuBknsgv2StRqku MINOR MMP-component [email protected] CODE_SMELL
AYODsI7Fknsgv2StRqac MAJOR MMP-component [email protected] CODE_SMELL
AYODsI7Nknsgv2StRqad MAJOR MMP-component [email protected] CODE_SMELL
AYODsI-Qknsgv2StRqai MAJOR MMP-component [email protected] CODE_SMELL
這里:
"[^[:blank:]]*$": 匹配以in input ( )開頭的文本$2,我們將其替換為空字串。column -t僅用于表格輸出,如果不需要,可以將其洗掉。
uj5u.com熱心網友回復:
考慮這種方法,如果您確定要洗掉的字符僅出現在第二列中(從此處借用和改編https://unix.stackexchange.com/questions/492500/awk-replace-one-character-only-在某列中)
awk '{{gsub("\",(\"component)?","", $2)}} 1' file.txt
gsub("\",\"component?","", $2)對于每個輸入行,將",("component)?第二個欄位中的所有內容替換為空白 - 這是一個正則運算式,表示 find",然后可選地括號中的部分:"component.?是可選的運算子1是列印內容的 awk 習慣用法$0(包含輸入記錄)
uj5u.com熱心網友回復:
第一個解決方案:考慮到您的字串","component總是會出現在同一個地方,請嘗試以下awk代碼。此代碼將僅按照所示示例保留欄位之間的空格。
awk '
match($0,/","*[^[:space:]]*/){
print substr($0,1,RSTART-1) sprintf("%-"(RLENGTH) "s",OFS) substr($0,RSTART RLENGTH)
next
}
1
' Input_file
第二種解決方案:使用 GNUawk使用match函式和正則運算式,它確實認為您的字串將僅按照所示示例出現在第二個欄位中,這也會處理 Input_file 中的空格并將它們保留在輸出中。這是使用的正則運算式的在線演示。
awk '
match($0,/^([^[:space:]] [[:space:]] )([^"]*)(","*[^[:space:]]*)(.*$)/,arr){
print arr[1] arr[2] sprintf("%-"length(arr[3]) "s",OFS) arr[4]
next
}
1
' Input_file
uj5u.com熱心網友回復:
您可以按照以下方式使用 GNU sed 來完成此任務,讓file.txt內容成為
AYOnanl3knsgv2StRr44 CRITICAL","component MMP-FileService [email protected] CODE_SMELL
AYOnanl3knsgv2StRr45 CRITICAL","component MMP-FileService [email protected] CODE_SMELL
AYOnanl3knsgv2StRr46 CRITICAL","component MMP-FileService [email protected] CODE_SMELL
AYOnanl3knsgv2StRr47 CRITICAL","component MMP-FileService [email protected] CODE_SMELL
AYOnanmeknsgv2StRr48 MAJOR", MMP-FileService [email protected] CODE_SMELL
AYOnanm-knsgv2StRr4- BLOCKER", MMP-FileService [email protected] CODE_SMELL
AYOnanm6knsgv2StRr49 MAJOR", MMP-FileService [email protected] CODE_SMELL
AYOnannKknsgv2StRr4_ BLOCKER", MMP-FileService [email protected] BUG
AYODwmuBknsgv2StRqkr MINOR", MMP-FileService [email protected] CODE_SMELL
AYODwmuBknsgv2StRqkt MINOR", MMP-FileService [email protected] CODE_SMELL
AYODwmuBknsgv2StRqks MINOR", MMP-component [email protected] CODE_SMELL
AYODwmuBknsgv2StRqku MINOR", MMP-component [email protected] CODE_SMELL
AYODsI7Fknsgv2StRqac MAJOR", MMP-component [email protected] CODE_SMELL
AYODsI7Nknsgv2StRqad MAJOR", MMP-component [email protected] CODE_SMELL
AYODsI-Qknsgv2StRqai MAJOR", MMP-component [email protected] CODE_SMELL
然后
sed 's/"[^[:space:]]*//' file.txt
給出輸出
AYOnanl3knsgv2StRr44 CRITICAL MMP-FileService [email protected] CODE_SMELL
AYOnanl3knsgv2StRr45 CRITICAL MMP-FileService [email protected] CODE_SMELL
AYOnanl3knsgv2StRr46 CRITICAL MMP-FileService [email protected] CODE_SMELL
AYOnanl3knsgv2StRr47 CRITICAL MMP-FileService [email protected] CODE_SMELL
AYOnanmeknsgv2StRr48 MAJOR MMP-FileService [email protected] CODE_SMELL
AYOnanm-knsgv2StRr4- BLOCKER MMP-FileService [email protected] CODE_SMELL
AYOnanm6knsgv2StRr49 MAJOR MMP-FileService [email protected] CODE_SMELL
AYOnannKknsgv2StRr4_ BLOCKER MMP-FileService [email protected] BUG
AYODwmuBknsgv2StRqkr MINOR MMP-FileService [email protected] CODE_SMELL
AYODwmuBknsgv2StRqkt MINOR MMP-FileService [email protected] CODE_SMELL
AYODwmuBknsgv2StRqks MINOR MMP-component [email protected] CODE_SMELL
AYODwmuBknsgv2StRqku MINOR MMP-component [email protected] CODE_SMELL
AYODsI7Fknsgv2StRqac MAJOR MMP-component [email protected] CODE_SMELL
AYODsI7Nknsgv2StRqad MAJOR MMP-component [email protected] CODE_SMELL
AYODsI-Qknsgv2StRqai MAJOR MMP-component [email protected] CODE_SMELL
說明:"使用空字串替換第一個和所有后續的非空白字符,即洗掉它。假設:"確實只出現在第二列,您不需要保持列對齊。
(在 GNU sed 4.2.2 中測驗)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/519921.html
標籤:linux壳awksed
下一篇:遞回/詳盡地將點插入字串
