0

I could write a shell script to do it in a loop, but is there a one line solution using the terminal ?

2
  • Maybe with something else, but not with cut. cut just writes to stdout. There is only one stdout. Therefore you can't create 2 files. – Celada Dec 12 '16 at 10:37
  • There might be a way of doing this by combining cut and tee or maybe other tools but for that we would need some sample input data and required output. please be more explicit in your questions. – mazs Dec 12 '16 at 10:41
5

Not with one cut command. You could do it with awk:

awk -F '\t' '{print $1 > "file1"; print $2 > "file2"}' < file

Or for every field:

awk -F '\t' '{for (i = 1; i <= NF; i++) print $i > "file" i}' < file

Or to avoid reading the file twice, with pipes and 2 cut invocations, with a shell like AT&T ksh, zsh or bash with support for process substitution:

< file tee >(cut -f2 > file2) | cut -f1 > file1

Beware that ksh and bash don't wait for that cut command running in the process substitution, so in those shells, file2 may not be complete by the time you run the next command after that.

0
-1

I think you can do like this:

cut -f1 input | tee output1 | cut -f1 > output2

If your desire filed is first and second columns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.