1

I have a file with this content:

xyz[0]
xyz[1]
xyz[2]
xyz[3]
abc[0]
abc[1]
abc[2]

Each value is separated by a newline. I want to separate them into two different files based on whether the number in square brackets is even or odd.

2

Using Awk, split on characters [ and ] and then modulo divide the second field:

$ awk -F'[][]' '{f = $2%2 ? "odd" : "even"; print > f}' file

$ head odd even
==> odd <==
xyz[1]
xyz[3]
abc[1]

==> even <==
xyz[0]
xyz[2]
abc[0]
abc[2]

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.