I guess the general question I have is, is it possible to give awk a field separator, store one of the tokens in a variable, then give awk another field separator, and store one of the tokens in a second variable, then print out both the variable values? It seems like the variables store a reference to the $nth token, not the value itself.
The specific example I had in mind more or less follows this form: {Animal}, {species} class
Cat, Felis catus MAMMAL
Dog, Canis lupus familiaris MAMMAL
Peregrine Falcon, Falco peregrinus AVIAN
...
and you want it to output something like:
Cat MAMMAL
Dog MAMMAL
Peregrine Falcon AVIAN
...
Where what you want is something that fits the form: {Animal} class
with something being enclosed in {}’s meaning it could have any number of spaces.
My original idea was I would have something like this:
cat test.txt | awk '{FS=","}; {animal=$1}; {FS=" "}; {class=$NF}; {print animal, class}; > animals.txt
I expect the variable “animal” to store what’s to the left of the comma, and “class” to to have the class type of that animal, so MAMMAL, etc. But what ends up happening is that only the last used Field separator is applied, so this would break for things that have spaces in the name, like Peregrine Falcon, etc.
so it would look something like
Cat, MAMMAL
Dog, MAMMAL
Peregrine AVIAN
One way using
awk:Results: