user3742796
user3742796

Reputation: 65

How to manipulate text lines and Create two separate files for odd and even

How to manipulate text lines and Create two separate files for odd and even ?

file 1:

iitmc01n01
iitmc01n03
.
.
iitmc01n71

file 2:

iitmc01n02
iitmc01n04
.
.
iitmc01n72

Upvotes: 0

Views: 65

Answers (2)

Jotne
Jotne

Reputation: 41460

This should do:

awk '{print > ("file"(substr($1,length($1))%2?"1":"2"))}' input

The %2 is used on last digit to see if number are odd or even

Added some more parentheses, thanks to info from Ed

Upvotes: 2

Wintermute
Wintermute

Reputation: 44053

I'd say

awk '/[13579]$/ { print > "file1"; next } { print > "file2" }' inputfile

This will print lines in inputfile that end with 1, 3, 5, 7, or 9 to file1 and all others to file2.

Upvotes: 1

Related Questions