-1

i have a file with a list of Identifier that i have to grep from another file. I have to do this for a pairs of files (several like 100 pairs). The files look like:

A.not.txt.duplicated:

chr1_937201_937501  
chr1_937501_937801  

A.not.txt:

chr1_937201_937501  329 1  
chr1_937201_937501  144 1  
chr1_937501_937801  329 1  
chr1_937501_937801  144 1  
chr1_1875601_1875901    764 1  
chr1_1875601_1875901    183 1  
chr1_2246701_2247001    377 1  
chr1_2246701_2247001    194 1  
chr1_937201_937501  0   0  
chr1_937201_937501  0   0  
chr1_937501_937801  0   0  
chr1_937501_937801  0   0  
chr1_1875601_1875901    0 0  
chr1_1875601_1875901    0   0  
chr1_2246701_2247001    0   0  
chr1_2246701_2247001    0   0  

Output:

chr1_937201_937501  329 1  
chr1_937201_937501  144 1  
chr1_937501_937801  329 1  
chr1_937501_937801  144 1

I know that I can do by:

grep -f A.not.txt.duplicated A.not.txt > A.not.txt.duplicated  

but the problem is that i have several pairs and i would like to make this in a loop.

Any Idea is really appreciated.

  • Which name patterns do the files have? A.not.txt.duplicated ... Z.not.txt.duplicated and A.not.txt ... Z.not.txt? – FloHe Jan 26 '17 at 11:17
  • ENCFF002DDT.bed.sorted.bed.length_peaks.score.bed.header.not.txt.duplicated and ENCFF002DDT.bed.sorted.bed.length_peaks.score.bed.header.not.txt then another pair for example is ENCFF002DDS.bed.sorted.bed.length_peaks.score.bed.header.not.txt.duplicated ENCFF002DDS.bed.sorted.bed.length_peaks.score.bed.header.not.txt the file ending with *.duplicated has the list of words to grep.. – fusion.slope Jan 26 '17 at 11:18
  • 3
    Your command grep -f A.not.txt.duplicated A.not.txt > A.not.txt.duplicated won't work at all because A.not.txt.duplicated will be truncated to zero bytes before grep even starts. – Wildcard Jan 26 '17 at 11:18
  • 1
    @fusion.slope The first thing the shell does when it parses the line is to look for redirections like >. It then goes on to truncate the file that you redirect to. Then it executes the command. – Kusalananda Jan 26 '17 at 11:25
  • 1
    @Wildcard Oh now i see, the name of the file with the list of words to grep is the same of the name of the file in the output: "A.not.txt.duplicated". Now i have understood. I cannot put the same name of a file in the input as name of file in the output. Finally! – fusion.slope Feb 25 '17 at 23:08
3

Use join

for f in *.not.txt.duplicated; do
  join <(sort "$f") <(sort "${f%.duplicated}") > "$f.out"
done

If you really want the ".duplicated" files overwritten by the results, as indicated in your example command, add && mv "$f.out" "$f" onto the end of the join line.

  • 1
    for f in *.not.txt; do grep -f "$f.duplicates" "$f" > "$f.out" ; done – JJoao Jan 26 '17 at 13:33
  • but the guys said that it is not possible to use grep -f for these reasons: "Your command grep -f A.not.txt.duplicated A.not.txt > A.not.txt.duplicated won't work at all because A.not.txt.duplicated will be truncated to zero bytes before grep even starts." Is there a difference on this and in what i wrote in my question? – fusion.slope Jan 26 '17 at 13:53
  • anyway @JJoao it works your way, i do not see the point of why they removed me 2 points saying that it is wrong. Thanks i will make this clear to them. – fusion.slope Jan 26 '17 at 14:05
  • @fusion.slope, your main idea is correct, and your question makes sense. But any command writing to is arguments (like grep ... a > a) will not work. (all our solutions are writing to a different file). Wildcard, sorry to put comments in your answer... – JJoao Jan 26 '17 at 15:01
  • Well the concept is correct @JJoao also you re-direct the output in your loop and this is exactly what i said that work for one file. Anyway does not matter. – fusion.slope Jan 26 '17 at 15:10

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.