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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:13:57+00:00 2026-06-14T09:13:57+00:00

In AWK, is it possible to specify ranges of fields? Example. Given a tab-separated

  • 0

In AWK, is it possible to specify “ranges” of fields?

Example. Given a tab-separated file “foo” with 100 fields per line, I want to print only the fields 32 to 57 for each line, and save the result in a file “bar”. What I do now:

awk 'BEGIN{OFS="\t"}{print $32, $33, $34, $35, $36, $37, $38, $39, $40, $41, $42, $43, $44, $45, $46, $47, $48, $49, $50, $51, $52, $53, $54, $55, $56, $57}' foo > bar

The problem with this is that it is tedious to type and prone to errors.

Is there some syntactic form which allows me to say the same in a more concise and less error prone fashion (like “$32..$57”) ?

  • 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-14T09:13:58+00:00Added an answer on June 14, 2026 at 9:13 am

    You can do it in awk by using RE intervals. For example, to print fields 3-6 of the records in this file:

    $ cat file
    1 2 3 4 5 6 7 8 9
    a b c d e f g h i
    

    would be:

    $ gawk 'BEGIN{f="([^ ]+ )"} {print gensub("("f"{2})("f"{4}).*","\\3","")}' file
    3 4 5 6
    c d e f
    

    I’m creating an RE segment f to represent every field plus it’s succeeding field separator (for convenience), then I’m using that in the gensub to delete 2 of those (i.e the first 2 fields), remember the next 4 for reference later using \3, and then delete what comes after them. For your tab-separated file where you want to print fields 32-57 (i.e. the 26 fields after the first 31) you’d use:

    gawk 'BEGIN{f="([^\t]+\t)"} {print gensub("("f"{31})("f"{26}).*","\\3","")}' file
    

    The above uses GNU awk for it’s gensub() function. With other awks you’d use sub() or match() and substr().

    EDIT: Here’s how to write a function to do the job:

    gawk '
    function subflds(s,e,   f) {
       f="([^" FS "]+" FS ")"
       return gensub( "(" f "{" s-1 "})(" f "{" e-s+1 "}).*","\\3","")
    }
    { print subflds(3,6) }
    ' file
    3 4 5 6
    c d e f
    

    Just set FS as appropriate. Note that this will need a tweak for the default FS if your input file can start with spaces and/or have multiple spaces between fields and will only work if your FS is a single character.

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

Sidebar

Related Questions

it's possible to change N (for example second occurrence) in file using one-line sed/awk
Is it possible to specify the separator field FS in binary for awk? I
Possible Duplicate: Truncate stdin line length? I have been looking for an awk or
Possible Duplicate: Get the newest file based on timestamp I primary want to select
Possible Duplicate: How can I write in nth column of a file in awk?
Is it possible to grab text from a online text file via grep/cat/awk or
Possible Duplicate: Parsing errors in awk blocks I am getting a parsing error while
Possible Duplicate: eliminate unwanted output using awk and sed The following contents are there
In Unix/linux shell we can: seq 0 100 | head -10 | awk 'NF%2==0'
is it possible to write a bash script that can read in each line

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.