Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8408623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:41:03+00:00 2026-06-09T23:41:03+00:00

I guess the general question I have is, is it possible to give awk

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T23:41:04+00:00Added an answer on June 9, 2026 at 11:41 pm

    One way using awk:

    awk -F, '{ n = split($2,array," "); printf "%s, %s\n", $1, array[n] }' file.txt
    

    Results:

    Cat, MAMMAL
    Dog, MAMMAL
    Peregrine Falcon, AVIAN
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is more of a general design question I guess... I have an Ajax
general question it can be in c i guess also if i have (
I guess my general question which I googled to no luck: Is there a
Prior to asking this question here, I have googled aroung. In general, people suggest
Ok i guess this is a bit of a general question really as opposed
I'm working in AS3, but I guess this could be a general question, so
I have this weird question. I would like to know if it is possible
This is more of a general question as I have found relatively little information
I have a question about C# generics and delegate: First I describe the general
I have a general question about jQuery. Lets say I have the following code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.