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

  • Home
  • SEARCH
  • 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 9168563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:39:16+00:00 2026-06-17T15:39:16+00:00

I am attempting to extract data between the nth occurrence of 2 patterns. Pattern

  • 0

I am attempting to extract data between the nth occurrence of 2 patterns.

Pattern 1: CardDetail

Pattern 2: ]

The input file, input.txt has thousands of lines that vary in what each line contains. The lines I’m concerned with grabbing data from will always contain CardDetail somewhere in the line. Finding the matching lines is easy enough using awk, but pulling the data between each match and placing it onto seperate lines each is where I’m falling short.

input.txt contains data about network gear and any attached/child devices. It looks something like this:

DeviceDetail [baseProductId=router-5000, cardDetail=[CardDetail [baseCardId=router-5000NIC1, cardDescription=Router 5000 NIC, cardSerial=5000NIC1], CardDetail [baseCardId=router-5000NIC2, cardDescription=Router 5000 NIC, cardSerial=5000NIC2]], deviceSerial=5000PRIMARY, deviceDescription=Router 5000 Base Model]
DeviceDetail [baseProductId=router-100, cardDetail=[CardDetail [baseCardId=router-100NIC1, cardDescription=Router 100 NIC, cardSerial=100NIC1], CardDetail [baseCardId=router-100NIC2, cardDescription=Router 100 NIC, cardSerial=100NIC2]], deviceSerial=100PRIMARY, deviceDescription=Router 100 Base Model]

* UPDATE: I forgot to mention in the initial post that I also need the device’s PARENT serials (deviceSerial) listed with them as well. *

What I would like the output.txt to look like is something like this:

"router-5000NIC1","Router 5000 NIC","5000NIC1","5000PRIMARY"
"router-5000NIC2","Router 5000 NIC","5000NIC2","5000PRIMARY"
"router-100NIC1","Router 100 NIC","100NIC1","100PRIMARY"
"router-100NIC2","Router 100 NIC","100NIC2","100PRIMARY"

The number of occurrences of CardDetail on a single line could vary between 0 to hundreds depending on the device. I need to be able to extract all of the data by field between each occurrence of CardDetail and the next occurrence of ] and transport them to their own line in a CSV format.

  • 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-17T15:39:17+00:00Added an answer on June 17, 2026 at 3:39 pm

    Here is an example that uses regular expressions. If there are minor variations in the text format, this will handle them. Also this collects all the values in an array; you could then do further processing (sort values, remove duplicates, etc.) if you wish.

    #!/usr/bin/awk -f
    
    BEGIN {
        i_result = 0
        DQUOTE = "\""
    }
    
    {
        line = $0
        for (;;)
        {
            i = match(line, /CardDetail \[ **([^]]*) *\]/, a)
            if (0 == i)
                break
            # a[1] has the text from the parentheses
            s = a[1]
            # replace from this: a, b, c   to this:  "a","b","c"
            gsub(/ *, */, "\",\"", s)
            s = DQUOTE s DQUOTE
    
            results[i_result++] = s
            line = substr(line, RSTART + RLENGTH - 1)
        }
    }
    
    END {
        for (i = 0; i < i_result; ++i)
            print results[i]
    }
    

    P.S. Just for fun I made a Python version.

    #!/usr/bin/python
    
    import re
    import sys
    
    DQUOTE = "\""
    
    pat_card = re.compile("CardDetail \[ *([^]]*) *\]")
    pat_comma = re.compile(" *, *")
    
    results = []
    
    def collect_cards(line, results):
        while True:
            m = re.search(pat_card, line)
            if not m:
                return
            len_matched = len(m.group(0))
            s = m.group(1)
            s = DQUOTE + re.sub(pat_comma, '","', s) + DQUOTE
            results.append(s)
            line = line[len_matched:]
    
    if __name__ == "__main__":
        for line in sys.stdin:
            collect_cards(line, results)
    
        for card in results:
            print card
    

    EDIT: Here’s a new version that also looks for “deviceID” and puts the matched text as the first field.

    In AWK you concatenate strings just by putting them next to each other in an expression; there is an implicit concatenation operator when two strings are side by side. So this gets the deviceID text into a variable called s0, using concatenation to put double quotes around it; then later uses concatenation to put s0 at the start of the matched string.

    #!/usr/bin/awk -f
    
    BEGIN {
        i_result = 0
        DQUOTE = "\""
        COMMA = ","
    }
    
    {
        line = $0
        for (;;)
        {
            i = match(line, /deviceID=([A-Za-z_0-9]*),/, a)
            s0 = DQUOTE a[1] DQUOTE
            i = match(line, /CardDetail \[ **([^]]*) *\]/, a)
            if (0 == i)
                break
            # a[1] has the text from the parentheses
            s = a[1]
            # replace from this: foo=a, bar=b, other=c   to this:  "a","b","c"
            gsub(/[A-Za-z_][^=,]*=/, "", s)
            # replace from this: a, b, c   to this:  "a","b","c"
            gsub(/ *, */, "\",\"", s)
            s = s0 COMMA DQUOTE s DQUOTE
    
            results[i_result++] = s
            line = substr(line, RSTART + RLENGTH - 1)
        }
    }
    
    END {
        for (i = 0; i < i_result; ++i)
            print results[i]
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to extract a given pattern within a text file, however, the results
I'm attempting to extract a series of data values from a text file. The
I am attempting to extract all data from a file with one regex expression.
Actually I am attempting to extract the data from a PDF file but I
I am currently attempting to extract data from a mysql DB and then place
I'm attempting to extract data from log files and organise it systematically. I have
I am attempting to extract pitch data from an audio stream. From what I
I am attempting to extract Weibull distribution parameters (shape 'k' and scale 'lambda') that
I am attempting to extract all instances of a particular format from a string:
I'm playing around with Expressjs and am attempting to extract the page title from

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.