Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cut command for selecting multiple columns from different files and printing the output in a new file

Tags:

bash

I want to cut fields separately from two different files, file1.txt and file2.txt, and store the output in a new file, output.txt. I know how to do it for one file. Can someone help me out at this?

cut -d"," -f 1,3 file1.txt > output.txt

I want to do something like:

cut ( -d"," -f 1,3 file1.txt ) && ( -d"," -f 1,2 file2.txt ) > output.txt
like image 384
david watson Avatar asked Dec 04 '25 15:12

david watson


1 Answers

Maybe you're looking for this:

paste -d, <(cut -d, -f1,3 file1.txt) <(cut -d, -f1,2 file2.txt) > output.txt

That assumes that you want fields 1 and 3 of the first file and 1 and 2 of the second file to all appear on a single line of the output.

<(...) is process substitution (not a redirect), which creates a name for a pipe containing the output of the enclosed command. paste just pastes together lines from its arguments, using whatever character is provided with the -d argument as a delimiter. See man paste.

like image 67
rici Avatar answered Dec 07 '25 12:12

rici



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!