我有一个包含此代码的脚本
for i in {2..9}
do
grep "Node${i}\|01, source address = 00:00:00:00:00:0${i}" t1.txt > t2.txt
done
是否可以将循环从“9”扩展到十六进制 MAC 地址的“f”字符,以便也处理“a”到“f”情况?
答案1
只需为字母添加另一个大括号扩展:
for i in {2..9} {a..f}
do
grep "Node${i}\|01, source address = 00:00:00:00:00:0${i}" t1.txt > t2.txt
done
请注意,这可能不是您真正想要的。每次运行此代码时,它都会覆盖其中的内容,t2.txt
这意味着您只能看到最终迭代的结果。如果不匹配,即使其他内容匹配,您也会得到一个空文件。也许您想附加:
for i in {2..9} {a..f}
do
grep "Node${i}\|01, source address = 00:00:00:00:00:0${i}" t1.txt >> t2.txt
done
或者,更有可能的是,您根本不需要循环,而应该这样做:
grep -E 'Node[2-9a-f]|01, source address = 00:00:00:00:00:0[2-9a-f]' t1.txt > t2.txt