条件 grep/ack 搜索?
Conditional grep/ack searches?
我的意思是我要搜索"thing1",然后我要根据"thing1"的位置搜索"thing2"。我想按照它们在代码中的顺序在结果中显示它们。
例如。我在第 100 行找到 "thing1"。然后我想搜索出现在 "thing1" 之前的第一个 "thing2"。然后我想按 "thing2" 然后 "thing1" 的顺序显示这两个。我想为我找到的每个 "thing1" 实例执行此操作。
这样做的原因是我想搜索某些我知道会出现在列表中的字符串 (python),而且我也想知道列表的名称。所以我想我可以搜索字符串,然后还显示字符串之前出现的第一个“= [”符号。
所以如果一个文件有:
my_list = [
'item1',
'item2',
'item3',
]
my_other_list = [
'item4',
'item5',
'item3',
]
并创建一个查找 'item3'
的搜索,然后回头查找之前的 '= ['
那么输出应该是(不包括 grep 和 ack 将放置的行号):
my_list = [
'item3',
my_other_list = [
'item3',
我想你想要这个:
awk '/=/{thing2=[=10=]} /item3/{print thing2;print [=10=],"\n"}' YourFile
因此,每次您看到 =
,您就会记住这一行 thing2
。当您看到 item3
时,您打印最后看到的 thing2
和当前行。
示例输出
my_list = [
'item3',
my_other_list = [
'item3',
我的意思是我要搜索"thing1",然后我要根据"thing1"的位置搜索"thing2"。我想按照它们在代码中的顺序在结果中显示它们。
例如。我在第 100 行找到 "thing1"。然后我想搜索出现在 "thing1" 之前的第一个 "thing2"。然后我想按 "thing2" 然后 "thing1" 的顺序显示这两个。我想为我找到的每个 "thing1" 实例执行此操作。
这样做的原因是我想搜索某些我知道会出现在列表中的字符串 (python),而且我也想知道列表的名称。所以我想我可以搜索字符串,然后还显示字符串之前出现的第一个“= [”符号。
所以如果一个文件有:
my_list = [
'item1',
'item2',
'item3',
]
my_other_list = [
'item4',
'item5',
'item3',
]
并创建一个查找 'item3'
的搜索,然后回头查找之前的 '= ['
那么输出应该是(不包括 grep 和 ack 将放置的行号):
my_list = [
'item3',
my_other_list = [
'item3',
我想你想要这个:
awk '/=/{thing2=[=10=]} /item3/{print thing2;print [=10=],"\n"}' YourFile
因此,每次您看到 =
,您就会记住这一行 thing2
。当您看到 item3
时,您打印最后看到的 thing2
和当前行。
示例输出
my_list = [
'item3',
my_other_list = [
'item3',