subset columns from the 1st file using column names in 2nd file
5
votes
6
answers
304
views
I have two text files:
1st file is a Tab delimited file which looks like this:
chrom pos ref alt a1 a2 a3 a4
10 12345 C T aa bb cc dd
10 12345 C T aa bb cc dd
10 12345 C T aa bb cc dd
10 12345 C T aa bb cc dd
10 12345 C T aa bb cc dd
10 12345 C T aa bb cc dd
2nd file looks like this:
a1
a4
I want to extract those columns in the 1st file which are present in the 2nd file along with first 4 columns of the first file. So in the above case, the output will look like this:
chrom pos ref alt a1 a4
10 12345 C T aa dd
10 12345 C T aa dd
10 12345 C T aa dd
10 12345 C T aa dd
10 12345 C T aa dd
10 12345 C T aa dd
I want to do this in shell. How can I do this?
I have a bigger file than shown here, so I have many columns in 1st file
cut -f 1-4,$(grep -Fwf file2.txt <(head -1 file1.txt)) file1.txt
Asked by user3138373
(2589 rep)
Mar 22, 2024, 04:01 AM
Last activity: Mar 24, 2024, 03:25 AM
Last activity: Mar 24, 2024, 03:25 AM