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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:47:24+00:00 2026-06-14T12:47:24+00:00

I have an xml-like textfile which I would like to parse into an array.

  • 0

I have an xml-like textfile which I would like to parse into an array. The input file looks like this

<AA>
  <BB>content 1</BB>
</AA>
<AA>
  <BB>content 2</BB>
</AA>

I want the output to be like (meaning one whole AA-block per array element):

ARRAY[0]=<AA><BB>content 1</BB></AA>
ARRAY[1]=<AA><BB>content 2</BB></AA>

I tried

ARRAY=(`cat input.txt | grep -A 3 \<AA\>`)

but this only returns me one line per array element. Does anyone have an idea?

  • 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-14T12:47:25+00:00Added an answer on June 14, 2026 at 12:47 pm

    XML and shell scripts don’t mix very well. If you can, consider using a different file format or a different scripting language.

    (
        IFS=$'\n'
        ARRAY=($(grep -A 3 '<AA>' test.xml | awk '{printf "%s",$0} $0~"</AA>" {print}'))
    
        for MATCH in "${ARRAY[@]}"; do
            echo "$MATCH"
        done
    )
    

    Explanation:

    1. Setting IFS to \n controls how array elements are split apart. We don’t want them split on spaces or tabs, just new lines.
    2. ARRAY=($(COMMAND)) captures COMMAND’s output and takes each line as an array element (since we set IFS to \n).
    3. {printf "%s",$0} prints each line without the trailing newline.
    4. $0~"</AA>" {print} prints a newline whenever we see a closing tag </AA>.
    5. The whole thing is in parentheses to limit the scope of the $IFS change. We don’t want that change to be permanent; better to limit it to a sub-shell.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a sample xml file that looks like this: <Books> <Category Genre=Fiction BookName=book_name
I have a XML file, that looks like this inside: <Data> <INFO> .. ...
ok, so, i have an xml file that looks like this: <?xml version=1.0?> <Users>
I have XML file which is looks like: <Report> <Total> <RecordValues> <Record> <FieldValue fieldName=index
I have table with a XML column (called MetaData) which looks like this: <props>
I have an XML spec that looks like this: <Root> <Directory Name=SomeName> <TextFile Name=ExampleDocument.txt>This
I have an XML document that looks like this: <kmsg xmlns=http://url1 xmlns:env=url1 xmlns:xsi=http://www.w3.org/2001/XMLSchemainstance xsi:schemaLocation=http://location
I have xml like this: <rule> <word>I</word> <word>need</word> <word>more</word> <marker> <word>money</word> </marker> <word>now</word> </rule>
I have xml like this: <configurationData> <path name='b'> <path name='a'> <setting name='s1'> ![CDATA[XXXX]] </setting>
Let's suppose I have xml like this one: <Server Active=No> <Url>http://some.url</Url> </Server> C# class

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.