[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 339
  • Last Modified:

Looping through directories and running .txt files into a program

Hi,

I currently have .txt files saved in different folders in one directory.

I'm now trying to create a shell script which will run through my directory, go through all the folders and if there is a text file containing the word "FASTA" (all capitals) in the directory, it will run the following code:

formatdb -i input_textfilenameFASTA.txt -n output_nameDATABASE

The program turns the textfile into a database and creates three files: .phr , .psq and .pin

Therefore, say the shell script opens the first folder and comes across a text file with FASTA in the name:

file1FASTA.txt

therefore, the script would do the following:

formatdb -i file1FASTA.txt -n file1DATABASE

it would then put the database files in the same directory as the text file. If there are any more text files in the directory, it would then do the same with these. If there are no more files in the directory it would then move on to the next directory.

Would it be possible to achieve this through Shell scripting?

Thanks,

Stephen,
0
StephenMcGowan
Asked:
StephenMcGowan
1 Solution
 
farzanjCommented:
So there are multiple parts to your problem.

There is a command line parsing.  I am not sure you know how to use getopts or not.  That would take care of that part like -i -n.

Suppose you have file in a directory and you cd to that directory then you can iterate through those files like

for file in *
do
    if [[ $file =~ pin$ ]]
    then
         echo "$file with .pin extension"
    fi
done
0
 
TintinCommented:

#!/bin/bash
for file in $(find /some/dir -name "*FASTA.txt")
do
   echo "Processing $file"
   formatdb -i $file -n ${file/FASTA.txt/DATABASE}
done

Open in new window

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now