简体   繁体   English

grep在外壳程序脚本中使用python(循环)变量和字符串

[英]grep using a python (loop) variable and a string in a shell script

Context: I have an internal Linux command, let's say "signals", which will give output whatever the signals placed for that day by oozie. 上下文:我有一个内部Linux命令,比方说“信号”,它将输出oozie当天发出的任何信号。

For example: 例如:

$signals | grep -i empty
*/user/oozie/coordinator/signals/2017/10/12/oltp1/_insurance_transaction_empty*
*/user/oozie/coordinator/signals/2017/10/12/oltp2/_loan_transaction_empty*

$signals | grep -i maxid
*/user/oozie/coordinator/signals/2017/10/12/oltp1/_insurance_transaction_maxid*
*/user/oozie/coordinator/signals/2017/10/12/oltp1/_loan_transaction_maxid*

I am writing a python script which runs every day and will grep for "empty" and "maxid" signals per oltp and send an email. 我正在编写一个每天运行的python脚本,它将针对每个oltp进行 grep表示“空”和“最大”信号并发送电子邮件。

Let's say there are empty signal for oltp1 and maxid signal for oltp2. 假设oltp1有空信号,oltp2有maxid信号。 It will send an email with all oltps and signals. 它将发送包含所有oltps和信号的电子邮件。

I have a configfile which has all oltps in 1 column: 我有一个配置文件,其中所有oltps都在1列中:

oltp1
oltp2
soltp
doltp

Code: 码:

#!/usr/bin/python

import commands
import os

config_file="/home/xxx/config_file"
for var in confil_file:
    var=var.strip()
    print " checking with OLTP:"+var
    empty_cnt_loan=commands.getoutput("signals | grep -i empty | grep -i $var | wc -l")
    maxid_cnt_loan=commands.getoutput("signals | grep -i maxid | grep -i $var| wc -l")
    print empty_cnt_loan

attempts: tried with $var 尝试:用$ var尝试

ERROR: grep: write error:Broken Pipe

tried with %var empty_cnt_loan has value 0, where when i run the command in linux box, its has 2 value. 尝试使用%var empty_cnt_loan的值为0,当我在linux框中运行命令时,它的值为2。

I tried subprocess, but since I am new I couldn't figure out how to use it.. Any help would be much appreciated. 我尝试了子流程,但是由于我是新手,所以我不知道如何使用它。任何帮助将不胜感激。

Running a complex Bash pipeline to do things which are trivial in Python is an antipattern. 运行复杂的Bash管道来完成Python中琐碎的事情是一种反模式。 Just run signals once and loop over the output in Python. 只需运行一次signals并在Python中循环输出即可。

Assuming Python 3.6+ , 假设Python 3.6以上版本

#!/usr/bin/env python3

from subprocess import run, PIPE

with open("/home/xxx/config_file") as config:
    vars = [line.strip() for line in config.readlines()]
empty_cnt_loan = {x: 0 for x in vars}
maxid_cnt_loan = {x: 0 for x in vars}
p = run(['signals'], stdout=PIPE, check=True, universal_newlines=True)
for line in p.stdout.split('\n'):
    line = line.lower()
    for var in vars:
        if var in line:
            if 'maxid' in line:
                maxid_cnt_loan[var] += 1
            if 'empty' in line:
                empty_cnt_loan[var] += 1

As an aside, grep has an option -c to count the number of matches; 顺便说一句, grep有一个选项-c来计算匹配的数量。 but you really almost never need grep (or Awk, or sed , or cut , or a number of other standard utilities) when you have Python. 但是使用Python时,您几乎几乎不需要grep (或Awk或sedcut或许多其他标准实用程序)。

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

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