root@ROUTER:~# cat maccheck.txt
logread | egrep ': STA |DHCPACK' | awk '{print $1" "$2" "$3" "$9}' | sed -e 's/\( [0-9] \)/0\1/' | sed "s/.\{15\}/&:/; s/: /:/g" | cut -d : -f 1,2,4,5,6,7,8,9 | sed "s/.\{13\}/&X/;" | sed 's/:X/ /g' | sed 's/XX:XX:XX:XX:XX:XX/AAAA/g' | sort -u -r
当它在这里时就可以了:
root@ROUTER:~# logread | egrep ': STA |DHCPACK' | awk '{print $1" "$2" "$3" "$9}' | head -1
Sep 2 03:03:25 XX:XX:XX:XX:XX:XX
但如果我使用包含更多内容的脚本,那就不好了:
root@ROUTER:~# sh maccheck.txt |head -1
Sep0 4 13:13 AAAA
所以“Sep0”是坏的..如何将其修改为“Sep”?
root@ROUTER:~# logread | egrep ': STA |DHCPACK' | awk '{print $1" "$2" "$3" "$9}' | sed -e 's/\( [0-9] \)/0\1/' | head -1
Sep0 2 03:03:25 AAAA
所以问题是:
sed -e 's/\( [0-9] \)/0\1/'
问:我需要相同的输出,但“Sep”中没有“0”,该怎么做?
答案1
\1
将为您提供您的组(该\( \)
部分)中列出的所有内容。您的组包含空格,因此将放入零,然后添加“2”。
要修复,请更改为
sed -e 's/ \([0-9]\) / 0\1 /'
例子
前
$ cat sample.txt | sed -e 's/\( [0-9] \)/0\1/'
Sep0 2 03:03:25 XX:XX:XX:XX:XX:XX
后
$ cat sample.txt | sed -e 's/ \([0-9]\) / 0\1 /'
Sep 02 03:03:25 XX:XX:XX:XX:XX:XX