如何操作文本行并为奇数和偶数创建两个单独的文件
How to manipulate text lines and Create two separate files for odd and even
如何操作文本行并为奇数和偶数创建两个单独的文件?
文件 1:
iitmc01n01
iitmc01n03
.
.
iitmc01n71
文件 2:
iitmc01n02
iitmc01n04
.
.
iitmc01n72
我会说
awk '/[13579]$/ { print > "file1"; next } { print > "file2" }' inputfile
这会将 inputfile
中以 1、3、5、7 或 9 结尾的行打印到 file1
,将所有其他行打印到 file2
。
应该这样做:
awk '{print > ("file"(substr(,length())%2?"1":"2"))}' input
最后一位用%2
判断数字是奇数还是偶数
添加了更多括号,感谢 Ed
提供的信息
如何操作文本行并为奇数和偶数创建两个单独的文件?
文件 1:
iitmc01n01
iitmc01n03
.
.
iitmc01n71
文件 2:
iitmc01n02
iitmc01n04
.
.
iitmc01n72
我会说
awk '/[13579]$/ { print > "file1"; next } { print > "file2" }' inputfile
这会将 inputfile
中以 1、3、5、7 或 9 结尾的行打印到 file1
,将所有其他行打印到 file2
。
应该这样做:
awk '{print > ("file"(substr(,length())%2?"1":"2"))}' input
最后一位用%2
判断数字是奇数还是偶数
添加了更多括号,感谢 Ed
提供的信息