Linux

如何按特定順序排列兩個字元串並根據我的 grep 逐行計算值

  • December 1, 2017

假設我有 Python 字典文本,我將其編輯為人類可讀的。所以它現在逐行作為以下輸入。

輸入

{"case":"0901","emailed":"yes","vote":1,"accepted":"no"},
{"case":"0908","emailed":"yes","vote":8,1"accepted":"yes"},
{"case":"0911","emailed":"no","vote":10,1"accepted":"yes"},
{"case":"0090","emailed":"yes","vote":3,1"accepted":"no"},

** 以前格式的所有文本文件 **

所以我想 grep 包含yes在第一和no第二中的行

所以我期望輸出是這樣的

輸出

{"case":"0901","emailed":"yes","vote":1,"accepted":"no"},
{"case":"0090","emailed":"yes","vote":3,1"accepted":"no"},

我還無法找到按單詞順序進行grep的方法。

我的第二個問題是關於我的輸出?

如果我可以使用awk sum函式來計算總票數?這應該4,1來自輸出。

我有 python 字典文本

正確的Python字典恢復/處理:

我的資訊是:Python 就是 Python ……你不應該亂碼它的資料結構

recover_dict.py腳本:

import sys, re, ast
with open(sys.argv[1], 'r') as f:
   items = ast.literal_eval(re.sub(r"(\d+),(\d+)", "\\1.\\2,", f.read().replace('\n','')))
   sum = 0
   for d in items:
       if d['emailed'] == 'yes' and d['accepted'] == 'no':
           sum += d['vote']
           print(d)
print(sum)

用法:

python recover_dict.py file

輸出:

{'case': '0901', 'vote': 1, 'accepted': 'no', 'emailed': 'yes'}
{'case': '0090', 'vote': 3.1, 'accepted': 'no', 'emailed': 'yes'}
4.1

檢查這個:

列印需要的行

awk -F'[,:]' ' 
$4 ~ "yes" && $8 ~ "no" {
   print;
}' input.txt

輸出

{"case":"0901","emailed":"yes","vote":1,"accepted":"no"},
{"case":"0090","emailed":"yes","vote":3,1"accepted":"no"},

計算總和

awk -F'[,:]' ' 
$4 ~ "yes" && $8 ~ "no" {
   sum += $6"."$7;
}
END {
   print sum;
}' input.txt

輸出

4.1

引用自:https://unix.stackexchange.com/questions/408295