This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to extract data from multiple files

Hi all,

I am doing some data extraction but got some difficulty in extracting from multiple files, I really appreciate your help in this situation:

I have many patient profile in separate folders, e.g patient1; patient2; patient3; ...

Each folder contain data files: test1; test2; test3; test4

I would like to count the number of occurrences of the string "hgc" in each test file in each patient folder and summary this values in an only file following this form:

patient1 value1 value2 value3 value4 #(each value is corresponded to a test file, from test1 to test4)
patient2 value1 value2 value3 value4
patient3 value1 value2 value3 value4
...

I was trying to fill this form manually using grep and wc command, but it would take forever if number of patient is large.

I really appreciate any help, thanks a lot!

grep

1 answer

Something like:

for p in `ls -d patient*`
do
    echo -n $p >> some_file.txt
    for d in `ls $p/test*`
    do
        n=`grep -n hgc $d`
        echo -ne "\t$n" >> some_file.txt
    done
    echo -ne "\n" >> some_file.txt
done

That should be enough to get you started at least.

Thank you very much!

Log in to answer this question.