-1

I've a script with this code

for i in {2..9}
        do 
        grep "Node${i}\|01, source address = 00:00:00:00:00:0${i}" t1.txt > t2.txt
        done

Is it possible to expand the loop from "9" to the "f" character of the exadecimal MAC address, in order to handle the "a" to "f" cases too?

2
  • 1
    You mean for i in {2..9} {a..f}; do ...? Is that enough? That will just include the letters between a and f in your current locale.
    – terdon
    Jul 14 at 15:28
  • Yep that works, I mean I also bruteforced with a for i in 2 3 4 5 6 7 8 9 a b c d e f Not pretty but it works too
    – tesiman
    Jul 14 at 15:31
8

Just add another brace expansion for the letters:

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

Note that this is likely not what you really want. Each time this code runs, it will overwrite the contents of t2.txt which means you will only ever see the results for the final iteration. If that didn't match, you'll have an empty file even if something else did match. Maybe you want to append instead:

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

Or, more likely, you don't need a loop at all and should instead do this:

grep -E 'Node[2-9a-f]|01, source address = 00:00:00:00:00:0[2-9a-f]' t1.txt > t2.txt
0

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.