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 8630437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:03:56+00:00 2026-06-12T09:03:56+00:00

I am trying to set an awk variable field to several field at once.

  • 0

I am trying to set an awk variable field to several field at once.

Right now I can only set the variables one by one.

for line in `cat file.txt`;do
    var1=`echo $line | awk -F, '{print $1}'`
    var2=`echo $line | awk -F, '{print $2}'`
    var3=`echo $line | awk -F, '{print $3}'`

    #Some complex code....
done

I think this is costly cause it parses the linux variable several times. Is there a special syntax to set the variable at once? I know that awk has a BEGIN and END block but the reason I am trying to avoid the BEGIN and END block is to avoid nested awk.

I plan to place another loop and awk code in the #Some complex code…. part.

for line in `cat file.txt`;do
    var1=`echo $line | awk -F, '{print $1}'`
    var2=`echo $line | awk -F, '{print $2}'`
    var3=`echo $line | awk -F, '{print $3}'`

    for line2 in `cat file_old.txt`;do
        vara=`echo $line2 | awk -F, '{print $1}'`
        varb=`echo $line2 | awk -F, '{print $2}'`

        # Do comparison of $var1,var2 and $vara,$varb , then do something with either
    done
done
  • 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-12T09:03:57+00:00Added an answer on June 12, 2026 at 9:03 am

    You can use the IFS internal field separator to use a comma (instead of whitespace) and do the assignments in a while loop:

    SAVEIFS=$IFS;
    IFS=',';
    while read line; do
        set -- $line;
        var1=$1;
        var2=$2;
        var3=$3;
        ...
    done < file.txt
    
    IFS=$SAVEIFS;
    

    This will save a copy of your current IFS, change it to a , character, and then iterate over each line in your file. The line set -- $line; will convert each word (separated by a comma) into a numeric-variable ($1, $2, etc.). You can either use these variables directly, or assign them to other (more meaningful) variable names.

    Alternatively, you could use IFS with the answer provided by William:

    IFS=',';
    while read var1 var2 var3; do
        ...
    done < file.txt
    

    They are functionally identical and it just comes down to whether or not you want to explicitly set var1=$1 or have it defined in the while-loop’s head.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to set rules if a certain variable is put into place, can someone
Trying to set it so if a certain condition is met then one of
Hello guys. I trying set EditText to input only numbers and point ( .
I am trying set selectors as global variables or namespaces.. I am not really
how i can input detailed information about errors? i was trying set customErrors mode
I have div tag containing several ul tags. If I trying set CSS properties
Trying to set up one-way SSL with Weblogic 11g and running into an issue.
Trying to set up a basic backbone.js example - can't get events to fire.
Im trying set the single table inheritance model type in a form. So i
Trying to set up a self signed certificate, for our intranet's web services site.

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.