Search code examples
bashparallel.foreach

How to load files and all result put in separate files in bash?


I have a few *.afa format files. I need to delete gaps from them, and i have a tool for that. It's a fac. The command in fac is :

fac -g inputFile.afa outputFile.afa

And it's working perfectly. But now I need to automatically process 100xx files, and I want to script it. But in the result, I need to have an unchanged input file and result in file witch R in first place in the file name. For example:

Before:
1.afa 30kb
2.afa 30kb
3.afa 40kb

After:
1.afa 30kb
R1.afa 15kb
2.afa 30kb
R2.afa 15kb
3.afa 40kb
R3.afa 20kb

Bash is unfamiliar to me. I know I should learn it, and I will, but I need result fast. Now I just write this:

#!/bin/bash
ls *.afa | parallel "fac -g {} >{R.}.afa"

But: 1. I can't write a proper regex to print result in a new file in R in the first letter 2. Only the last result is saved in one file. That's don't generate a result for each file.

Can someone help me?


Solution

  • You should be able to do this in a single line within bash shell assuming you're in the folder where all the .afa files are:

    l=($(ls *.afa)); for f in ${l[@]}; do fac -g $f "R$f"; done
    # Should also work without "" around R$f.
    

    As @codeforester pointed out, this is better (safer):

    for f in *.afa; do fac -g $f "R$f"; done
    

    This can be expanded as:

    l=(*.afa) # bash array for all .afa files.
    for f in ${l[@]}; do # loop over array
        fac -g $f R$f; # Run original command for each $f
    done