extract the all columns from 1 file giving a list of another file
1
0
Entering edit mode
2.9 years ago
harry ▴ 40

I have 1 text file like this below :

X   119614423   119614883   intergenic  52  NA  hsa-intergenic_023278
X   123867152   123873856   XIAP    25  NA  hsa-XIAP_0027
X   129496750   129511983   SMARCA1 1   hsa_circ_0091477    hsa-SMARCA1_0029
X   134413488   134417302   PHF6    1   hsa_circ_0139788    hsa-PHF6_0006
X   135994786   136002213   SLC9A6  1   NA  hsa-SLC9A6_0005
X   14850505    14859334    FANCB   1   hsa_circ_0006971    NA
X   150795206   150795486   CD99L2  1   NA  NA
X   154349649   154353457   FLNA    1   hsa_circ_0091862    NA
X   154353001   154353727   FLNA    1   NA  NA
X   154361465   154362484   FLNA    2   NA  hsa-FLNA_0189
X   154768302   154769310   DKC1    1   hsa_circ_0007100    hsa-DKC1_0006
X   18506935    18510854    CDKL5   1   hsa_circ_0089980    hsa-CDKL5_0010
X   24732370    24743329    POLA1   1   hsa_circ_0140155    NA
X   24739375    24748992    POLA1   1   hsa_circ_0140156    NA
X   3825088 3899422 BX890604.1  1   NA  NA
X   47836946    47846321    ZNF81   1   NA  NA
X   53634236    53654131    HUWE1   1   NA  hsa-HUWE1_0007
X   53645311    53654131    HUWE1   1   hsa_circ_0001922    hsa-HUWE1_0003

I have another file that contains a list of the second column like below:

119614423
123867152
129496750
134413488
135994786
14850505
150795206
154349649
154353001
154361465
154768302
18506935
24732370
24739375
3825088
47836946
53634236
53645311

I want to extract all columns from file 1 which are present in file2.

So far I used the command:

grep -f file2.txt file1.txt >output.txt

But it only greps the last row of file 1, not whole which is present in file2.

Thanks in advance for your suggestions.

grep • 1.2k views
ADD COMMENT
0
Entering edit mode

I want to extract all columns from file 1 which are present in file2.

I suppose you mean "row" instead of "columns" ? If so, it is weird because it works for me (every row is returned, not just the last one). Can you try it again, double check file content, and see if it the issue persists ?

PS: in that context, I would use the -w option so that the full position is considered:

grep -wf file2.txt file1.txt > output.txt
ADD REPLY
0
Entering edit mode

I used that command which you suggested but again I got only the last row of the file1.

Thanks in advance

ADD REPLY
0
Entering edit mode

Have you tried with more simple examples. For instance file1 is:

1
2
3

and file 2 is:

1
3

then output.txt should be

1
3

Does it work ? if it does, then your files are probably corrupted in some way. If even that simple thing does not work, then its probably your grep that is broken.

ADD REPLY
2
Entering edit mode
2.9 years ago
ATpoint 85k

Exactly this one here: https://unix.stackexchange.com/a/458452/240890

ADD COMMENT
0
Entering edit mode

I used this command also which suggested in the link

awk 'NR==FNR {a[$1]++; next} $2 in a' file2.txt file1.txt >output.txt

But also I got the same output means only the last row I got in my output file.

thanks

ADD REPLY

Login before adding your answer.

Traffic: 1797 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6