3

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.

1 Answer 1

3

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]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .