期望使用grep的通配符中的某些字符数

时间:2018-03-29 07:30:36

标签: bash macos unix grep

我想要一张通配符,正好是64个字符。任何更少和行被忽略。任何更多和该行被忽略。

foo.txt我有以下内容:

.*  /Users/1337/X$
.*  /Users/1337/R$

bar.txt我有以下内容:

1121cfccd5913f0a63fec40a6ffd44ea64f9dc135c66634ba001d10bcf4302a2  /Users/1337/R
aa460da4af72c482231e28e688e01f2778a88ce31a08826899d54ef7183998b5  /  /Users/1337/R
4355a46b19d348dc2f57c046f8ef63d4538ebb936000f3c9ee954a27460dd865  /Users/1337/W
53c234e5e8472b6ac51c1ae1cab3fe06fad053beb8ebfd8977b010655bfdd3c3  /Users/1337/X

我尝试使用grep来过滤掉与/Users/1337/X/Users/1337/R完全匹配的行,其前面有一个SHA256哈希值,它总是正好是64个字符。这样可以防止grep意外地从bar.txt拉出第2行,R最后有/Usersgrep之前有两个空格。

这个命令是我得到的最接近的命令,但如前所述,这可能会导致cat bar.txt | grep -f foo.txt 找错线:

1121cfccd5913f0a63fec40a6ffd44ea64f9dc135c66634ba001d10bcf4302a2  /Users/1337/R
53c234e5e8472b6ac51c1ae1cab3fe06fad053beb8ebfd8977b010655bfdd3c3  /Users/1337/X

我希望它输出:

1121cfccd5913f0a63fec40a6ffd44ea64f9dc135c66634ba001d10bcf4302a2  /Users/1337/R
aa460da4af72c482231e28e688e01f2778a88ce31a08826899d54ef7183998b5  /  /Users/1337/R
53c234e5e8472b6ac51c1ae1cab3fe06fad053beb8ebfd8977b010655bfdd3c3  /Users/1337/X

但它输出了这个:

 import tkinter
from tkinter import *
class Main_frame():

    def __init__(self):
        self._title  = 'Radiobutton Test'
        self._resize = False
        self.set_mainframe_size_for_geometry(350, 200)
        self.create_mainframe()

    def create_mainframe(self):
        self.mainframe = Tk()
        self.mainframe.title(self._title)
        self.mainframe.geometry(self.get_mainframe_size_for_geometry())
        self.mainframe.resizable(self._resize, self._resize)

    def set_mainframe_size_for_geometry(self, mainframe_width, mainframe_height):
        self._mainframe_size_for_geometry = str(mainframe_width) + 'x' + str(mainframe_height)

    def get_mainframe_size_for_geometry(self):
        return self._mainframe_size_for_geometry


class Create_Radiobutton():

    def __init__(self,frame, name, positionx, positiony, group, value, controller):
        self.group                             = IntVar()
        self._set_frame_to_insert_radiobutton   = frame
        self._radiobutton_label                 = name
        self._radiobutton_horizontal_position   = positionx
        self._radiobutton_vertical_position     = positiony
        self._radiobutton_group                 = group
        self._radiobutton_value                 = value
        self.get_intvar()
        self.create_radiobutton()
        self._radiobutton_controller = controller

    def create_radiobutton(self):
        self.set_radiobutton(Radiobutton(self._set_frame_to_insert_radiobutton,
                                            text=self._radiobutton_label,
                                            variable=self.get_group(),
                                            value=self._radiobutton_value,
                                            command= lambda : self._radiobutton_controller.main_method(self.get_group().get())))
        self.get_radiobutton().place(x=self._radiobutton_horizontal_position,
                                     y=self._radiobutton_vertical_position)

    def set_radiobutton(self, radiobutton):
        self._radiobutton = radiobutton

    def get_radiobutton(self):
        return self._radiobutton

    def get_radiobutton_controller(self, radiobutton_controller):
        return self.get_radiobutton().cget(radiobutton_controller)

    # here is the method to set the IntVar() for each group
    def get_intvar(self):
        if self._radiobutton_group == 1:
            self.set_group1()
        elif self._radiobutton_group == 2:
            self.set_group2()
        elif self._radiobutton_group == 3:
            self.set_group3()

    def set_group1(self):
        self.group = IntVar()
    def set_group2(self):
        self.group = IntVar()
    def set_group3(self):
        self.group = IntVar()

    def get_group(self):
        return self.group


class Radiobutton_controller():

    """
            here is the constructor from radiobutton_controller
            with the list dictionarys_with_active_values
    """
    def __init__(self, button):
        self.button = button
        self.list_with_correct_values = [[], [], []]

    """
           from here i excecute the method
           self.get_radiobutton_value(intvar_value) to fill the list with 
           values
    """
    def main_method(self, intvar_value):
        self.get_radiobutton_value(intvar_value)
        print(self.list_with_correct_values)

    def get_checkbutton1(self):
        for element in self.button:
            return self.button['Checkbutton1'].get()

    def get_checkbutton2(self):
        for element in self.button:
            return self.button['Checkbutton1'].get()

    """
          in this method i put the value in the list
    """
    def get_radiobutton_value(self, intvar_value):
        if intvar_value > 0 and intvar_value <= 2:
            self.list_with_correct_values[0].clear()
            self.list_with_correct_values[0].append(intvar_value)
        if self.get_checkbutton1() == 1:
            if intvar_value > 2 and intvar_value <= 4:
                self.list_with_correct_values[1].clear()
                self.list_with_correct_values[1].append(intvar_value)
        else:
            self.list_with_correct_values[1].clear()
            self.list_with_correct_values[1].append(0)
        if self.get_checkbutton2() == 1:
            if intvar_value > 4 and intvar_value <= 6:
                self.list_with_correct_values[2].clear()
                self.list_with_correct_values[2].append(intvar_value)
        else:
            self.list_with_correct_values[2].clear()
            self.list_with_correct_values[2].append(0)

class Build_Application():

    def main(self):
        self.radiobutton_dictionary = {}
        self.radiobutton = {}
        self.controller = Radiobutton_controller(self.radiobutton_dictionary)
        self.mainframe = Main_frame()
        self.radio1 = Create_Radiobutton(self.mainframe.mainframe,'radio1', 10, 10, 1, 1, self.controller)
        self.radio2 = Create_Radiobutton(self.mainframe.mainframe,'radio2', 100, 10, 1, 2, self.controller)
        self.radio3 = Create_Radiobutton(self.mainframe.mainframe,'radio3', 10, 50, 2, 3, self.controller)
        self.radio4 = Create_Radiobutton(self.mainframe.mainframe,'radio4', 100, 50, 2, 4, self.controller)
        self.radio5 = Create_Radiobutton(self.mainframe.mainframe,'radio5', 10, 100, 3, 5, self.controller)
        self.radio6 = Create_Radiobutton(self.mainframe.mainframe,'radio6', 100, 100, 3, 6, self.controller)

        self.checkbox1 = IntVar()
        self.checkbox2 = IntVar()
        self.checkbutton1 = Checkbutton(self.mainframe.mainframe, text='Disable/Enable', variable=self.checkbox1, ).place(x=200, y=50)
        self.checkbutton1 = Checkbutton(self.mainframe.mainframe, text='Disable/Enable', variable=self.checkbox2, ).place(x=200, y=100)

        self.radiobutton_dictionary['Radio1'] = self.radio1
        self.radiobutton_dictionary['Radio2'] = self.radio2
        self.radiobutton_dictionary['Radio3'] = self.radio3
        self.radiobutton_dictionary['Radio4'] = self.radio4
        self.radiobutton_dictionary['Radio5'] = self.radio5
        self.radiobutton_dictionary['Radio6'] = self.radio6
        self.radiobutton['Radio1'] = self.radio1
        self.radiobutton['Radio2'] = self.radio2
        self.radiobutton['Radio3'] = self.radio3
        self.radiobutton['Radio4'] = self.radio4
        self.radiobutton['Radio5'] = self.radio5
        self.radiobutton['Radio6'] = self.radio6
        self.radiobutton_dictionary['Checkbutton1'] = self.checkbox1
        self.radiobutton_dictionary['Checkbutton2'] = self.checkbox2

        self.mainframe.mainframe.mainloop()

app = Build_Application()
app.main()

我该如何解决这个问题?

Mac OS X Yosemite,bash 3.2.57(1)-release

1 个答案:

答案 0 :(得分:1)

首先确保在foo.txt中使用锚点以确保匹配完整的行:

cat foo.txt

^.{64}  /Users/1337/X$
^.{64}  /Users/1337/R$

然后使用此grep命令获取结果:

grep -Ef foo.txt bar.txt

1121cfccd5913f0a63fec40a6ffd44ea64f9dc135c66634ba001d10bcf4302a2  /Users/1337/R
53c234e5e8472b6ac51c1ae1cab3fe06fad053beb8ebfd8977b010655bfdd3c3  /Users/1337/X
-E命令中支持扩展正则表达式需要

grep选项。