1

I have two lists of files ending with two different extensions and I would like to concatenate them in pairs, in a loop. The file names look like these:

These are the files

a.ID, b.ID, c.ID, d.ID     
a.value, b.value, c.value, d.value  

intuitively I would do:

for i in *.ID;  
do  
     for j in *.value;  
do  
     cat $i $j > $i.txt ; done  
done  

The problem is that I would like to merge a.ID with a.value and b.ID with b.value and in this way they are merged randomly. Like a.value with b.ID etc..

Any idea? Thanks in advance

Samples input a.ID (for example):

chr1_237301_237601  176 1  
chr1_237601_237901  176 1  
chr1_237901_238201  176 1 

Samples ending with a.value (for example):

chr1_1_301      0   0  
chr1_301_601    0   0  
chr1_601_901    0   0  
chr1_901_1201   0   0  
chr1_1201_1501  0   0  

output:

chr1_237301_237601  176 1    
chr1_237601_237901  176 1   
chr1_237901_238201  176 1  
chr1_1_301      0   0    
chr1_301_601    0   0   
chr1_601_901    0   0  
chr1_901_1201   0   0  
chr1_1201_1501  0   0
  • 3
    Show sample input and expected output. – Dan Jan 25 '17 at 17:33
  • 1
    Just a question - how do you want to merge them ? Line by line ? So line 1 from A.ID get's joined with line 1 from A.value ? – Sergiy Kolodyazhnyy Jan 25 '17 at 17:34
  • Hey, I have added the two input and the output. Thanks – fusion.slope Jan 25 '17 at 17:41
  • yes they come always in pairs – fusion.slope Jan 25 '17 at 18:34
7

You don't need two loops. You need a single loop, through a, b, c etc. Like this:

for i in *.ID; do
    b=${i%%.ID}
    cat "$i" "$b".value >"$b".txt
done
  • Hi, I was trying to understand your code because now am trying to make a similar script. Could you explain a bit? Because basically i have to perform a grep from a list of words with this command: grep -f A B and I have to do the same as before. The problem is that one file is like in this way: A.not.txt and the other is A.not.txt.duplicated. So the grep in the loop would be: grep -f A.not.tx A.not.tx.duplicated. Any idea? Or I have to open a new post? Thanks. – fusion.slope Jan 26 '17 at 10:50
  • 1
    if *.ID is match to "aaa.ID" file then b=${i%%.ID} is extract "aaa" part from $i so next command become cat "aaa.ID" "aaa".value ... – mug896 Jan 28 '17 at 6:08
0

Just because it's possible to do so, here's find + bash approach.

$ ls
a.ID  a.value

$ find -type f -iname "*.ID" -exec bash -c 'base=$(basename -s ".ID" "$@");cat "$@" "$base".value > "$base".merged' sh   "{}" \;

$ ls
a.ID  a.merged  a.value

$ cat a.merged                                                                                                           
chr1_237301_237601  176 1  
chr1_237601_237901  176 1  
chr1_237901_238201  176 1 
chr1_1_301      0   0  
chr1_301_601    0   0  
chr1_601_901    0   0 

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.