簡體   English   中英

如何使用Unix / Awk / grep提取此特定字符串

[英]How to extract this particular string using Unix/Awk/grep

我有一個看起來像這樣的日志文件

Connected to feeder version 2.1 09:28:30 29/03/2014 Loading Account 01234567EUR
09:28:30 29/03/2014 Loading Account 0123456755JPY
09:28:30 29/03/2014 Loading Account 0123426567INR
09:28:30 29/03/2014 Loading Account 012345698887USD
09:28:30 29/03/2014 Loading Account 012343422567EUR
09:28:30 29/03/2014 Account 0234456783388KRY not set up
09:28:30 29/03/2014 Account 0234454467888CNH not set up
09:28:30 29/03/2014 Error : Closing Balance of Account 02344567888GBP Doesn't match

我想提取存在期末余額不匹配或帳戶未設置的帳號,然后將這些帳戶放入新文件中以進行進一步處理。第一步是我使用了grep -il'not set up ',但是在那之后我如何提取帳號,該模式似乎是非常隨機的(不確定是否可以基於分隔符使用awk)只有可以確定帳號的后3個字符的模式才是貨幣。 因此可以為此使用egrep和regex。 謝謝

這是awk一種方法:

$ awk '
/not set up/ {
    for(i=1;i<=NF;i++) 
        if($i~/Account/) print $(i+1)":Not Set Up" > "Review.txt"
}
/Error/ {
    for(i=1;i<=NF;i++)
        if($i~/Account/) print $(i+1)":Mismatch" > "Review.txt"
}' file

這將創建以下文件:

$ cat Review.txt
0234456783388KRY:Not Set Up
0234454467888CNH:Not Set Up
02344567888GBP:Mismatch

我將使用sed,而沒有grep:

sed -n "
    s/.* Closing Balance of Account \(.*\) Doesn't match/\1/p;
    s/.* Account \(.*\) not set up/\1/p
  "

調整口味,例如,如果您想在任一情況下打印一些東西,以識別哪個帳戶存在問題。

您可以按以下方式使用grep語句來獲取所需的帳號:

grep 'not set up' file.txt | grep -Po '\d+[A-Z]{3}'
grep 'Error' file.txt | grep -Po '\d+[A-Z]{3}'

另一種方法是在命令行中短缺帳號,即帳號:

awk -F'^.*Account|[ \t]*' '/Error|set/{print $3}' file

或連同原因:

awk -F'^.*Account[ \t]*' '!/Loading/{print $2}' file

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM