generate file names in loop

7,425

HELP 1

Due to the whitespace you need quoting:

INPUT= "${file##*/}" OUTPUT= "${file%.txt}"_exd.txt

It seems a bit strange, though, that there is a space between INPUT= and the file name.

HELP 3

If you have a file input_1.txt then ${file%.txt}_1.txt becomes input_1_1.txt.

I would match the exact file name:

for file in ~/Desktop/parent\ folder/*/input_1.txt; do
    bioTool INPUT1= "${file}" INPUT2= "${file%_1.txt}_2.txt" OUTPUT= "${file%%.*}"
done

HELP 4

That is no shell stuff at all.

changing into subdirectories

for file in ~/Desktop/parent\ folder/*/input_1.txt; do
    subdir="${file%/*}"
    pushd "$subdir" &>/dev/null
    file="${file##*/}"
    bioTool INPUT1= "${file}" INPUT2= "${file%_1.txt}_2.txt" OUTPUT= "${file%%.*}"
    popd &>/dev/null
done
Share:
7,425

Related videos on Youtube

ozarka
Author by

ozarka

Updated on September 18, 2022

Comments

  • ozarka
    ozarka over 1 year

    DISCLAIMER: I am using Mac OSX. The bash script is in a different directory from the files that require processing. From here my struggle begins…

    HELP 1: I have multiple files each housed in a subdirectory of a parent directory that needs to be fed as input files for my bioinformatics tool. I found that the for loop would work for this.

    for file in ~/Desktop/parent\ folder/*/*.txt;
    do exTool INPUT= ${file##*/} OUTPUT= ${file%.txt}_exd.txt 
    

    I am hoping to input the file (by cutting out the folder listing attached to $file) and then output a file, removing '.txt' and adding _exd.txt. The error I get is that the input is carrying the folder list and specifically the whitespace 'parent\ folder' is causing the wrong input info.

    HELP 2: As an extension of the first case, if I want to output the file into the same subdirectory as the input, do I have to add a special command?

    HELP 3: If I there are 2 files within each subdirectory (input_1.txt and input_2.txt) and I would like to input both into a single command. How would the script look if nested in a for loop?

    for file in ~/Desktop/parent\ folder/*/*.txt;
    do bioTool INPUT1= ${file%.txt}_1.txt INPUT2= ${file%.txt}_2.txt OUTPUT= ${file%%.*}
    

    For the output, notice that I am only needing the base filename as the tool will append a number at the end of the name to denote 1st and 2nd file output and the extension. For some reason, I feel like the section after 'in' in the for loop is being done incorrectly. It was a knee-jerk solution for having the script in a directory different from the files.

    Any help will be greatly appreciated, upvoted, liked, accepted, favorited. Thank you very much for your help.

    • muru
      muru over 8 years
      One question per post, please. Move the unrelated stuff like your supervisor's script to another post.
    • Hauke Laging
      Hauke Laging over 8 years
      Sorry, my earlier question (which I just deleted) didn't make sense because I misunderstood the path handling.
    • roaima
      roaima over 8 years
    • ozarka
      ozarka over 8 years
      @roaima Related but I was told to make a new post as the questions did not relate.