我是linux环境的新手,但由于我的新项目,我正在学习,我喜欢它,(来自vxwork时代)。现在我的问题是如何从这个命令过滤I2c地址" i2cdetect"使用bash脚本。我听说我可以使用SED或AWKD来扫描和搜索文本。 所以基本上我想得到每个i2c地址,比如0c,5b,5c,UU,6e,6f。
我感谢你的任何线索或帮助。
root @ plnx_aarch64:/#i2cdetect -y -r 3 0 1 2 3 4 5 6 7 8 9 a b c d e f
00: - - - - - - - - - 0c - - -
10: - - - - - - - - - - - - - - - -
20: - - - - - - - - - - - - - - - -
30: - - - - - - - - - - - - - - - -
40: - - - - - - - - - - - - - - - -
50: - - - - - - - - - - - 5b 5c - - -
60: - - - - - - - - - - - - - - 6e 6f
70:UU - - - - - - -
答案 0 :(得分:0)
我写了一个函数来做到这一点。我不确定是否有更好的方法,但这确实有用。
import re
def get_addresses(i2cdetect_output):
''' Takes output from i2cdetect and extracts the addresses
for the entries.
'''
# Get the rows, minus the first one
i2cdetect_rows = i2cdetect_output.split('\r\n')[1:]
i2cdetect_matrix = []
# Add the rows to the matrix without the numbers and colon at the beginning
for row in i2cdetect_rows:
i2cdetect_matrix.append(filter(None, re.split(' +', row))[1:])
# Add spaces to the first and last rows to make regularly shaped matrix
i2cdetect_matrix[0] = [' ', ' ', ' '] + i2cdetect_matrix[0]
i2cdetect_matrix[7] = i2cdetect_matrix[7][:-1] + [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
# Make a list of the addresses present
address_list = []
for i in range(len(i2cdetect_matrix)):
for j in range(len(i2cdetect_matrix[i])):
if i2cdetect_matrix[i][j] not in (' ', '--', 'UU'):
address_list.append(str(i) + str(format(j, 'x')))
if i2cdetect_matrix[i][j] == 'UU':
address_list.append('UU')
return address_list