我有以下包含2列的文件:
A:B:IP:80 apples
C:D:IP2:82 oranges
E:F:IP3:84 grapes
如何将文件拆分为2个其他文件,文件中的每一列都是这样的:
文件1
A:B:IP:80
C:D:IP2:82
E:F:IP3:84
文件2
apples
oranges
grapes
最佳答案
Perl 1-liner使用(滥用)print
转到STDOUT
即文件描述符1
,而warn
转到STDERR
即文件描述符2
:
# perl -n means loop over the lines of input automatically
# perl -e means execute the following code
# chomp means remove the trailing newline from the expression
perl -ne 'chomp(my @cols = split /\s+/); # Split each line on whitespace
print $cols[0] . "\n";
warn $cols[1] . "\n"' <input 1>col1 2>col2
当然,您可以只将
cut -b
与相应的列一起使用,但是随后您将需要两次读取文件。关于linux - Linux将文件分为两列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49332357/