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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:43:25+00:00 2026-05-27T17:43:25+00:00

I have an input file like this: SomeSection.Foo OtherSection.Foo OtherSection.Goo …and there is another

  • 0

I have an input file like this:

SomeSection.Foo
OtherSection.Foo
OtherSection.Goo

…and there is another file describing which object(s) belong to each section:

[SomeSection]
Blah
Foo
[OtherSection]
Foo
Goo

The desired output would be:

SomeSection.2   // that's because Foo appears 2nd in SomeSection
OtherSection.1  // that's because Foo appears 1st in OtherSection
OtherSection.2  // that's because Goo appears 2nd in OtherSection

(The numbers and names of sections and objects are variable)

How would you do such a thing in awk?

Thanks in advance,
Adrian.

  • 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-27T17:43:25+00:00Added an answer on May 27, 2026 at 5:43 pm

    One possibility:

    Content of script.awk (with comments):

    ## When 'FNR == NR', the first input file is in process.                                                                                                                                                                                     
    ## If line begins with '[', get the section string and reset the position                                                                                                                                                                           
    ## of its objects.                                                                                                                                                                                                                           
    FNR == NR && $0 ~ /^\[/ {                                                                                                                                                                                                                    
            object = substr( $0, 2, length($0) - 2 )                                                                                                                                                                                             
            pos = 0
            next
    }
    
    ## This section process the objects of each section. It saves them in
    ## an array. Variable 'pos' increments with each object processed.
    FNR == NR {
            arr_obj[object, $0] = ++pos
            next
    }
    
    ## This section process second file. It splits line in '.' to find second
    ## part in the array and prints all.
    FNR < NR {
            ret = split( $0, obj, /\./ )
            if ( ret != 2 ) {
                    next
            }
            printf "%s.%d\n", obj[1], arr_obj[ obj[1] SUBSEP obj[2] ]
    }
    

    Run the script (important the order of input files, object.txt has sections with objects and input.txt the calls):

    awk -f script.awk object.txt input.txt
    

    Result:

    SomeSection.2
    OtherSection.1
    OtherSection.2
    

    EDIT to a question in comments:

    I’m not an expert but I will try to explain how I understand it:

    SUBSEP is a character to separate indexes in an array when you want to use different values as key. By default is \034, although you can modify it like RS or FS.

    In instruction arr_obj[object, $0] = ++pos the comma joins all values with the value of SUBSEP, so in this case would result in:

    arr_obj[SomeSection\034Blah] = 1
    

    At the end of the script I access to the index using explicity that variable arr_obj[ obj[1] SUBSEP obj[2], but with same meaning that arr_obj[object, $0] in previous section.

    You can also access to each part of this index splitting it with SUBSEP variable, like this:

    for (key in arr_obj) {                     ## Assign 'string\034string' to 'key' variable
        split( key, key_parts, SUBSEP )        ## Split 'key' with the content of SUBSEP variable.
        ...
    }
    

    with a result of:

    key_parts[1] -> SomeSection
    key_parts[2] -> Blah
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an input file something like this: some line some other line another
I have this use case of an xml file with input like Input: <abc
I have a file like this (which is space delimited): AX-18 Chr1_419085 1 41908545
So let's say i have one input like this: <input id=test multiple=true type=file name=image_name[]
I have a code like this- file:input.h struct Address{ int a; int b; };
Hi, I have a input that looks like this : <input type=file id=ModelViewAd.Files[0] name=ModelViewAd.Files[0]
Hi, I have a couple of input type file elements like this : <input
I have some input file that looks like this: asdfasfasfas.... asdfasdfasd.... asdfasdf.... asdfsadf... I
I have a file which looks like this: 1 2 AA 4 5 AA
I have a string like this: String string ='{{INPUT FILE, NUMBER OF RECORDS},{Item File,

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.