I have the following file that contains 2 columns :
JavaScript
x
A:B:IP:80 apples
C:D:IP2:82 oranges
E:F:IP3:84 grapes
How is possible to split the file in 2 other files, each column in a file like this:
File1
JavaScript
A:B:IP:80
C:D:IP2:82
E:F:IP3:84
File2
JavaScript
apples
oranges
grapes
Advertisement
Answer
Perl 1-liner using (abusing) the fact that print
goes to STDOUT
, i.e. file descriptor 1
, and warn
goes to STDERR
, i.e. file descriptor 2
:
JavaScript
# 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
You could, of course, just use cut -b
with the appropriate columns, but then you would need to read the file twice.