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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:00:02+00:00 2026-06-08T12:00:02+00:00

I typically work with large XML files, and generally do word counts via grep

  • 0

I typically work with large XML files, and generally do word counts via grep to confirm certain statistics.

For example, I want to make sure I have at least five instances of widget in a single xml file via:

cat test.xml | grep -ic widget

Additionally, I just like to be able to log the line that widget appears on, ie:

cat test.xml | grep -i widget > ~/log.txt

However, the key information I really need is the block of XML code that widget appears in. An example file may look like:

<test> blah blah
  blah blah blah
  widget
  blah blah blah
</test>

<formula>
  blah
  <details> 
    widget
  </details>
</formula>

I am trying to get the following output from the sample text above, ie:

<test>widget</test>

<formula>widget</formula>

Effectively, I’m trying to get a single line with the highest level of markup tags that apply to a block of XML text/code that is surrounding the arbitrary string, widget.

Does anyone have any suggestions for implementing this via a command-line one liner?

Thank you.

  • 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-08T12:00:04+00:00Added an answer on June 8, 2026 at 12:00 pm

    A non-elegant way using both sed and awk:

    sed -ne '/[Ww][Ii][Dd][Gg][Ee][Tt]/,/^<\// {//p}' file.txt | awk 'NR%2==1 { sub(/^[ \t]+/, ""); search = $0 } NR%2==0 { end = $0; sub(/^<\//, "<"); printf "%s%s%s\n", $0, search, end }'
    

    Results:

    <test>widget</test>
    <formula>widget</formula>
    

    Explanation:

    ## The sed pipe:
    
    sed -ne '/[Ww][Ii][Dd][Gg][Ee][Tt]/,/^<\// {//p}'
    ## This finds the widget pattern, ignoring case, then finds the last, 
    ## highest level markup tag (these must match the start of the line)
    ## Ultimately, this prints two lines for each pattern match
    
    ## Now the awk pipe:
    
    NR%2==1 { sub(/^[ \t]+/, ""); search = $0 }
    ## This takes the first line (the widget pattern) and removes leading
    ## whitespace, saving the pattern in 'search'
    
    NR%2==0 { end = $0; sub(/^<\//, "<"); printf "%s%s%s\n", $0, search, end }
    ## This finds the next line (which is even), and stores the markup tag in 'end'
    ## We then remove the slash from this tag and print it, the widget pattern, and
    ## the saved markup tag
    

    HTH

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

Sidebar

Related Questions

I need to pre-compress some very large html/xml/json files (large data dumps) using either
I typically work on large chunks of code on a project, and from time
I typically work on small-sized projects that involve only 2-3 people working on the
I am looking for an efficient indexed persistent data structure. I typically work in
I've to work with a large database that contains measurement targets as well as
I have some large files (hundreds of MB) that I need to search for
I'm developing a Grails application to a school work. Typically, this is the URL
Typically, access to Azure workers is done via endpoints that are defined in the
We get a large amount of data from our clients in pdf files in
I have written a converter that takes openstreetmap xml files and converts them to

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.