Python grep就像if语句一样

时间:2012-06-25 19:33:35

标签: python find grep

我有以下python脚本,它接受一些输入并将它们放在文件中,具体取决于sys.argv。我想添加一些重复的条目检查...如果一个值通过sys.argv传递到一个文件但它已经存在什么都不做,否则打印到文件行。

我正在考虑使用subprocess并使用系统find / grep命令(分别用于windows / linux),但是我无法让这个测试工作。

欢迎任何想法/代码。

由于

# Import Modules for script
import os, sys, fileinput, platform, subprocess

# Global variables
hostsFile = "hosts.txt"
hostsLookFile = "hosts.csv"
hostsURLFileLoc = "urls.conf"

# Determine platform
plat = platform.system()

if plat == "Windows":

# Define Variables based on Windows and process
    #currentDir = os.getcwd()
    currentDir = "C:\\Program Files\\Splunk\\etc\\apps\\foo\\bin"
    hostsFileLoc = currentDir + "\\" + hostsFile
    hostsLookFileLoc = currentDir + "\\..\\lookups\\" + hostsLookFile
    hostsURLFileLoc = currentDir + "\\..\\default\\" + hostsURLFileLoc
    hostIP = sys.argv[1]
    hostName = sys.argv[2]
    hostURL = sys.argv[3]
    hostMan = sys.argv[4]
    hostModel = sys.argv[5]
        hostDC = sys.argv[6]



    # Add ipAddress to the hosts file for python to process 
    with open(hostsFileLoc,'a') as hostsFilePython:

#       print "Adding ipAddress: " + hostIP + " to file for ping testing"
#       print "Adding details: " + hostIP + "," + hostName + "," + hostURL + "," + hostMan + "," + hostModel + " to file"
        hostsFilePython.write(hostIP + "\n")

    # Add all details to the lookup file for displaying on-screen and added value
    with open(hostsLookFileLoc,'a') as hostsLookFileCSV:

        hostsLookFileCSV.write(hostIP + "," + hostName + "," + hostURL + "," + hostMan + "," + hostModel + "," + hostDC +"\n")

    if hostURL != "*":  

        with open(hostsURLFileLoc,'a+') as hostsURLPython:

            hostsURLPython.write("[" + hostName + "]\n" + "ping_url = " + hostURL + "\n") 

更新:我正在尝试一个基于steveha提供的小片段,我遇到了os.rename部分的问题

>>> import os
>>> import sys
>>> in_file = "inFile.txt"
>>> out_file = "outFile.txt"
>>> dir = "C:\\Python27\\"
>>> found_in_file = False
>>> with open(in_file) as in_f, open(out_file,"w") as out_f:
...     for line in in_f:
...             if line.endswith("dax"):
...                     found_in_file = True
...     if not found_in_file:
...             out_f.write("192.168.0.199\tdax\n")
...     os.rename( os.path.join(dir, in_f), os.path.join(dir,out_f))

我收到以下错误。

Traceback (most recent call last):
  File "<stdin>", line 7, in <module>
  File "C:\Python27\lib\ntpath.py", line 73, in join
    elif isabs(b):
  File "C:\Python27\lib\ntpath.py", line 57, in isabs
    s = splitdrive(s)[1]
  File "C:\Python27\lib\ntpath.py", line 125, in splitdrive
    if p[1:2] == ':':
TypeError: 'file' object is not subscriptable

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

直接在Python中执行“grep”任务会更容易,也更快:

with open("filename") as f:
    for line in f:
        if "foo" in line:
           ... # do something to handle the case of "foo" in line

这是一个程序,它将一个名为“dax”的主机添加到/etc/hosts

import sys
_, in_fname, out_fname = sys.argv

found_in_file = False
with open(in_fname) as in_f, open(out_fname, "w") as out_f:
    for line in lst:
        if line.endswith("dax"):
            found_in_file = True
        out_f.write(line)
    if not found_in_file:
        out_f.write("192.168.0.199\tdax\n")

您指定了两个文件名,输出文件名将获得/etc/hosts的副本。如果已在/etc/hosts中找到系统名称“dax”,则副本将是准确的;否则会附加一行。

您可以通过使用正则表达式来检测特定行来扩展此想法,并且可以通过编写不同的行而不是原始行来编辑行。

此程序使用正则表达式查找/etc/hosts文件中包含192.168.0.10192.168.0.59范围内的所有条目。重写这些行以将其移至192.168.1.*,其中*是原始地址,不变。

import re
import sys
_, in_fname, out_fname = sys.argv

pat = re.compile(r'^192.168.0.(\d+)\s+(\S+)')

with open(in_fname) as in_f, open(out_fname, "w") as out_f:
    for line in in_f:
        m = pat.search(line)
        if m:
            x = int(m.group(1))
            if 10 <= x < 60:
                line = "192.168.1." + str(x) + "\t" + m.group(2) + "\n"
        out_f.write(line)

如果您已成功编写输出文件并且没有错误,则可以使用os.rename()将新文件重命名为原始文件名,从而覆盖旧文件。如果你发现你不需要更改旧文件中的任何行,你可以删除新文件而不是重命名它;如果你总是重命名,即使你没有真正改变任何东西,你也会更新你文件的修改时间戳。

编辑:这是运行最后一个代码示例的示例。假设您将代码放入名为move_subnet.py的文件中,然后在Linux或其他* NIX上,您可以像这样运行它:

python move_subnet.py /etc/hosts ./hosts_copy.txt

如果您使用的是Windows,它将是这样的:

python move_subnet.py C:/Windows/system32/drivers/etc/hosts ./hosts_copy.txt

请注意,Windows可以在文件名中使用反斜杠或正斜杠。我在这个例子中使用了正斜杠。

您的输出文件将在当前目录中为hosts_copy.txt

相关问题