擅长:python、mysql、java
<p>保持简单-将文本文件拆分为行,将行拆分为单词,检查第一个单词是否在您感兴趣的单词列表中。你知道吗</p>
<pre><code>results = []
first_words = ['interface', 'description', 'channel-group']
input_file = 'switch.txt'
with open(input_file, 'r') as switch_file:
for line in switch_file.readlines():
words_in_line = line.split()
# There should be at least 1 word in the line
if 0 < len(words_in_line):
first_word = words_in_line[0]
if any(first_word in s for s in first_words):
results.append(line.rstrip())
print("\n".join(results))
</code></pre>
<p>输出:</p>
<pre><code>interface Port-channel10
interface GigabitEthernet0/0
description ** Uplink to C1 **
interface GigabitEthernet0/1
description ** Uplink to C2 **
channel-group 10 mode auto
interface GigabitEthernet0/2
description ** Downlink to NetAuto **
interface GigabitEthernet0/3
channel-group 10 mode auto
interface GigabitEthernet1/0
interface GigabitEthernet1/1
</code></pre>