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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:06:03+00:00 2026-05-27T16:06:03+00:00

I work with XML files containing book data. When investigating data corruption issues I

  • 0

I work with XML files containing book data. When investigating data corruption issues I often have to extract the whole records which include a particular string.

I am struggling to do this with my very limited knowledge of bash scripting and total lack of knowlwdge of other programming languages such as perl.

I have standard user access to a Linux box (RHEL 4) with no prospect of getting permission to install anything not already present.

Using standard tools/languages available on this box, can anyone explain how I might look for a particular string and extract any whole records from the file which might contain it?

E.g. to extract the whole records which contain ‘Smith’ from the following file.

Example data:

<File>
<Product>
<Ref>1</Ref>
<Title>My Life</Title>
<Series>Life Stories</Series>
<Author>John Smith</Author>
<Price>5.99</Price>
</Product>
<Product>
<Ref>2</Ref>
<Title>A Story</Title>
<Author>Fred Bloggs</Author>
<Price>16.99</Price>
</Product>
<Product>
<Ref>3</Ref>
<Title>Book 1</Title>
<Author>Jane Smith</Author>
<Price>10.99</Price>
</Product>
</File>

Required output:

<Product>
<Ref>1</Ref>
<Title>My Life</Title>
<Series>Life Stories</Series>
<Author>John Smith</Author>
<Price>5.99</Price>
</Product>
<Product>
<Ref>3</Ref>
<Title>Book 1</Title>
<Author>Jane Smith</Author>
<Price>10.99</Price>
</Product>

That is to say everything between the <Product> </Product> tags for the records containing the string ‘Smith’.

The records may contain different numbers of tags but will always be enclosed in <Product> </Product> tags.

I appreciate the perfect result may not be possible every time without using more specialist tools but I simply don’t have them available to me. Anything which gets me close would be great.

I’m thinking the script would read each record in the file, look for the string within each record in turn and redirect those records which match to an output. However, I am struggling to find the answer anywhere.

Many thanks for any help you can offer.

  • 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-27T16:06:03+00:00Added an answer on May 27, 2026 at 4:06 pm

    this should work for your example:

     awk 'BEGIN{RS="<[/]?Product>"} /Smith/{print "<Product>",$0,"</Product>"}' file
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

At work we are being asked to create XML files to pass data to
I have a task at work that involves converting legacy SGM files into XML.
I have 2 Xaml files, one containing a DataTemplate which has a resource definition
I would like to work with internal .xlsx files (styles.xml). I was using <oXygen/>
At work here, we have a box serving XML feeds to business partners. Requests
A major part of our work is creating and manipulating certain XML files, for
I have two xml files and I need to create one xsd for both.
So I have a maven project which produces a jar package containing some ant
Here's my problem.I have 2 xmlfiles with identical structure, with the second xml containing
We have lot of configuration files used in our application. Atleast 100 different xml

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.