1

I have this command:

awk -F'\t' '$3 ~ "Aspergillus fumigatus" {print}' $krakenfile > Aspergillus_fumigatus_lines.txt

It is working very fine, It just extracts every line in the file $krakenfile that contain the word "Aspergillus fumigatus" in its third column.

Now, I want to introduce another file ($fungalnames) that contains multiple lines, every line contains names of species and I want to apply the command on every line of the new file.

I tried this:

while IFS= read -r specie_name
do 
awk -F'\t' '$3 ~ "$specie_name" {print}' $krakenfile > "${specie_name}_lines.txt"
done < $fungalnames

Anyway, the output files are created (I had 8 species names in my $fungalnames file and there are 8 output files, every file is named to a line, but all the files are empty !!

I tried multiple times, and I changed syntaxes, but nothing is working.

I think I am escaping an important thing, can anyone help me please! Thank you in advance!

  • 2
    Does this answer your question? [Difference between single and double quotes in Bash](https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash) – tjm3772 Jul 11 '23 at 15:02
  • Variables don't expand in single quotes, so `"$specie_name"` is a literal string because your whole awk program is wrapped in single quotes. You need to pass it in as an awk variable. – tjm3772 Jul 11 '23 at 15:03
  • You can pass the variable to awk using `awk -v awk_var_name="$bash_var_name" ...` and then use it as `awk_var_name` (no `$`!) inside the (single quoted) awk script. – Socowi Jul 11 '23 at 15:20
  • 1
    If the sole purpose of the `bash/while/read` loop is to make a set of `awk` calls, then the whole block of code can be replaced with a single, and more efficient, `awk` call; my [answer to your previous question](https://stackoverflow.com/a/76663454) provides one example; if that answer (at the link) answers this question then you may want to delete this question (as a duplicate of your previous Q&A); – markp-fuso Jul 11 '23 at 15:57
  • Put the variable `$specie_name` outside the single-quotes, like this: `' single-quoted part'"$specie_name"'second single quoted part'`, or don't use single quotes in the first place. You could escape the `$3` using a backslash; no single quotes needed. – user1934428 Jul 12 '23 at 05:54

0 Answers0