Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to use two different Field Separators in awk and store values from both in variables?
    text
    copied!<p>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.</p> <p>The specific example I had in mind more or less follows this form: <em>{Animal}, {species} class</em></p> <pre><code>Cat, Felis catus MAMMAL Dog, Canis lupus familiaris MAMMAL Peregrine Falcon, Falco peregrinus AVIAN ... </code></pre> <p>and you want it to output something like:</p> <pre><code>Cat MAMMAL Dog MAMMAL Peregrine Falcon AVIAN ... </code></pre> <p>Where what you want is something that fits the form: <em>{Animal} class</em></p> <p>with something being enclosed in {}'s meaning it could have any number of spaces.</p> <p>My original idea was I would have something like this:</p> <pre><code>cat test.txt | awk '{FS=","}; {animal=$1}; {FS=" "}; {class=$NF}; {print animal, class}; &gt; animals.txt </code></pre> <p>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.</p> <p>so it would look something like</p> <pre><code>Cat, MAMMAL Dog, MAMMAL Peregrine AVIAN </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload