1
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

it OK when it's here:

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

but it's bad if I use the script that has further things in it:

root@ROUTER:~# sh maccheck.txt |head -1
Sep0 4 13:13 AAAA

so the "Sep0" is bad.. how to modify it to be "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

So the problem is with:

sed -e 's/\( [0-9] \)/0\1/'

Q: I need the same output, but without the "0" in "Sep", how to do it?

4

\1 will give you everything listed in your group (the \( \) section). Your group includes the spaces, so the zero will be put in, then the " 2 " will be added.

To fix, change to

sed -e 's/ \([0-9]\) / 0\1 /'

Example

before

$ cat sample.txt | sed -e 's/\( [0-9] \)/0\1/' 
Sep0 2 03:03:25 XX:XX:XX:XX:XX:XX

after

$ cat sample.txt | sed -e 's/ \([0-9]\) / 0\1 /'
Sep 02 03:03:25 XX:XX:XX:XX:XX:XX

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.