我是linux环境的新手,但由于我的新项目,我正在学习,我喜欢它(来自vxwork时代)。现在我的问题是如何使用bash脚本从命令"i2cdetect“中过滤I2c地址。我听说我可以使用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
发布于 2018-02-17 02:14:00
我写了一个函数来做这件事。我不确定是否有更好的方法,但这似乎确实有效。
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_listhttps://stackoverflow.com/questions/48797011
复制相似问题