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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:37:22+00:00 2026-05-15T13:37:22+00:00

I have to deal with various input files with a number of fields, arbitrarily

  • 0

I have to deal with various input files with a number of fields, arbitrarily arranged, but all consistently named and labelled with a header line. These files need to be reformatted such that all the desired fields are in a particular order, with irrelevant fields stripped and missing fields accounted for. I was hoping to use AWK to handle this, since it has done me so well when dealing with field-related dilemmata in the past.

After a bit of mucking around, I ended up with something much like the following (writing from memory, untested):

# imagine a perfectly-functional BEGIN {} block here

NR==1 {
  fldname[1] = "first_name"
  fldname[2] = "last_name"
  fldname[3] = "middle_name"
  maxflds = 3

  # this is just a sample -- my real script went through forty-odd fields

  for (i=1;i<=NF;i++) for (j=1;j<=maxflds;j++) if ($i == fldname[j]) fldpos[j]=i
}

NR!=1 {
  for (j=1;j<=maxflds;j++) {
    if (fldpos[j]) printf "%s",$fldpos[j]
    printf "%s","/t"
  }
  print ""
}

Now this solution works fine. I run it, I get my output exactly how I want it. No complaints there. However, for anything longer than three fields or so (such as the forty-odd fields I had to work with), it’s a lot of painfully redundant code which always has and always will bother me. And the thought of having to insert a field somewhere else into that mess makes me shudder.

I die a little inside each time I look at it.

I’m sure there must be a more elegant solution out there. Or, if not, perhaps there is a tool better suited for this sort of task. AWK is awesome in it’s own domain, but I fear I may be stretching it’s limits some with this.

Any insight?

  • 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-05-15T13:37:22+00:00Added an answer on May 15, 2026 at 1:37 pm

    The only suggestion that I can think of is to move the initial array setup into the BEGIN block and read the ordered field names from a separate template file in a loop. Then your awk program consists only of loops with no embedded data. Your external template file would be a simple newline-separated list.

    BEGIN {while ((getline < "fieldfile") > 0) fldname[++maxflds] = $0}
    

    You would still read the header line in the same way you are now, of course. However, it occurs to me that you could use an associative array and reduce the nested for loops to a single for loop. Something like (untested):

    BEGIN {while ((getline < "fieldfile") > 0) fldname[$0] = ++maxflds}
    
    NR==1 {
        for (i=1;i<=NF;i++) fldpos[i] = fldname[$i]
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I do have to deal with very large plain text files (over 10 gigabytes,
I often have to deal with XML documents that contain namespaced elements, but doesn't
Here's the deal. I have an application with three tabs. Through various interactions with
I have been reading the various blog posts on how to deal with a
Some of us programmers have to deal with graphic files every once and awhile...
I have a piece of network code that throws various exception which are all
it might be a duplication, but in eclipse, how can I deal with various
I have to deal with text files in a motley selection of formats. Here's
I have a number of data classes representing various entities. Which is better: writing
I have to deal with an API which need to be provided a DataSource

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.