简体   繁体   中英

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

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM