简体   繁体   English

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

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

I have a log file which somewhat looks like this 我有一个看起来像这样的日志文件

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

I want to extract the account numbers where there is a closing balance mismatch or if the account is not set-up and put those accounts into a new file for my further processing.The first step is i have used grep -il 'not set up' but after that how do I extract the account numbers, The pattern seems to be very random(not sure if I can use awk based on delimeter) Only pattern that is for sure is last 3 characters of an account Number is currency. 我想提取存在期末余额不匹配或帐户未设置的帐号,然后将这些帐户放入新文件中以进行进一步处理。第一步是我使用了grep -il'not set up ',但是在那之后我如何提取帐号,该模式似乎是非常随机的(不确定是否可以基于分隔符使用awk)只有可以确定帐号的后3个字符的模式才是货币。 So is it possible to use egrep and regex for this. 因此可以为此使用egrep和regex。 Thanks 谢谢

Here is one way with awk : 这是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

This creates the following file: 这将创建以下文件:

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

I'd use sed, without grep: 我将使用sed,而没有grep:

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

Adjust to taste, eg if you want to print something next to either case to identify which accounts have which problem. 调整口味,例如,如果您想在任一情况下打印一些东西,以识别哪个帐户存在问题。

You can use the grep statements as follows to get the desired account numbers: 您可以按以下方式使用grep语句来获取所需的帐号:

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

Another way, shortish from the command line, just the account numbers: 另一种方法是在命令行中短缺帐号,即帐号:

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

Or together with the reason: 或连同原因:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM