0

I'm trying to write a short script that enters into a directory prints out an array of the file names without an extension in one column and the in the next column adds the extension .fna. Then exits the directory to enter the next directory.

For eg I have the file names

ERR163030.fastq
ERR163031.fastq

and I would like the out put to be:

ERR163030 ERR163030.fna
ERR163031 ERR163031.fna

This is the code I have so far.

SRA_files=(P*)

# loop that picks each directory in the array
for i in "${SRA_files[@]}"
do
  #Go into directory 
   pushd "$i"; 
  #Make array  
   fastq_names=(*.fastq);
  # Exit directory 
  popd;
  # Print in main directory containing all the sub directories 
  printf "%s\n" "${fastq_names[*]%.*}" "${fastq_names[*]%.*}.fna" > "$i"_names.txt
done 

1 Answer 1

1

Since you want to put the names with and without the extension on the same line, using printf '%s\n' doesn't work as intended (when passing the two forms as distinct arguments). That said, it's straightforward to just use a loop:

#!/usr/bin/env bash
shopt -s nullglob                    ## avoid misbehavior when no matches exist
for dir in P*/; do                   ## / suffix ensures we only match directories
  fastq_names=( "$dir"*.fastq )
  for name in "${fastq_names[@]##*/}"; do
    printf '%s %s\n' "${name%.*}" "${name%.*}.fna"
  done >"${dir%/}"_names.txt
done

If we set up a test case as follows:

tempdir=$(mktemp -d "${TMPDIR:-/tmp}/test.XXXXXX") && cd "$tempdir"
mkdir -p P123; touch P123/{ERR163030,ERR163031}.fastq

...output in P123_names.txt is, as expected:

ERR163030 ERR163030.fna
ERR163031 ERR163031.fna
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.