0

I have a big size file like 5GB with .gz. Inside that file, we have few XML files that contains values that I want to search and extract just in case if those values are there.

For example I want to extract the tags that contains the name NOOSS and also the subcontent of this tags like <pmJobId>, <requestedJobState>, <reportingPeriod>, <jobPriority> from the the .gz file

<Pm xmlns="urnCmwPm">
    <pmId>1</pmId>
    <PmJob>
        <pmJobId>NOOSSCONTROLExample</pmJobId>
        <requestedJobState>ACTIVE</requestedJobState>
        <reportingPeriod>FIVE_MIN</reportingPeriod>
        <jobType>MEASUREMENTJOB</jobType>
        <jobPriority>HIGH</jobPriority>
        <granularityPeriod>FIVE_MIN</granularityPeriod>
        <jobGroup>Sla</jobGroup>
        <reportContentGeneration>CHANGED_ONLY</reportContentGeneration>
        <MeasurementReader>
            <measurementReaderId>mr_2</measurementReaderId>
            <measurementSpecification struct="MeasurementSpecification">
                <measurementTypeRef>Anything</measurementTypeRef>
            </measurementSpecification>
            <thresholdRateOfVariation>PER_SECOND</thresholdRateOfVariation>
        </MeasurementReader>
        <MeasurementReader>
            <measurementReaderId>mr_1</measurementReaderId>
            <measurementSpecification struct="MeasurementSpecification">
                <measurementTypeRef>ManagedElement=1,SystemFunctions=1,Pm=1,PmGroup=OSProcessingLogicalUnit,MeasurementType=CPULoad.Total</measurementTypeRef>
            </measurementSpecification>
            <thresholdRateOfVariation>PER_SECOND</thresholdRateOfVariation>
        </MeasurementReader>
    </PmJob>
</Pm>

I was using cat *gz 1 zgrep -a "PmJobId" but the output only show the <pmJobId> value and not the rest of the information or tags.

Please your help, I'm noobie on this.

Im using CentOS - RedHat Linux.

Thanks

2
  • 1
    A single compressed file can't contain multiple other files unless it's an archive of some sort. Do I understand you correctly that you want the PmJob node and all its sub-nodes if the corresponding pmJobId node's value contains the substring NOOSS? And you want the actual XML? It would benefit if you could show the result, given the example document you include in the question.
    – Kusalananda
    Sep 29, 2021 at 16:37
  • The namespace declaration, i.e xmlns="urnCmwPm" contains an invalid URI.
    – fpmurphy
    Sep 29, 2021 at 20:27

2 Answers 2

0

Assuming that the XML document in file.xml is well-formed and correct in all ways (the example in the question has a faulty namespace declaration), then you would be able to extract the part of the document that corresponds to the PmJob nodes that have a pmJobID value that contains the substring NOOSS with the command-line XML parser xmlstarlet.

xmlstarlet sel -t -c '//PmJob[contains(pmJobId,"NOOSS")]' -nl file.xml

This command selects all PmJob nodes with a child node, pmJobId, whose value contains the substring NOOSS. The utility will return a copy of the selected PmJob node and all its child nodes.

4
  • Hi, thanks for your help, is there any way or another similar command? This command xmlstarlet is not supported in RedHat. Thank you. Sep 29, 2021 at 17:40
  • 1
    @JuanPabloBustamanteLuna I think you will find that it is supported.
    – Kusalananda
    Sep 29, 2021 at 17:49
  • Thank you for share this. Unfortunately, the server is not able to install additional packages since the server is under production and for security reasons is not connected to the internet to get and install packages. Sep 29, 2021 at 18:37
  • I'm going to try in my local VM with RHEL. I will keep you posted about this possible resolution. Thanks to all. Sep 29, 2021 at 18:54
0

Assuming that the XML document is well-formed and valid, you can use the xmllint utility to output the requisite nodes.

$ xmllint --xpath '//PmJob[contains(pmJobId,"NOOSS")]' file.xml
<PmJob>
        <pmJobId>NOOSSCONTROLExample</pmJobId>
        <requestedJobState>ACTIVE</requestedJobState>
        <reportingPeriod>FIVE_MIN</reportingPeriod>
        <jobType>MEASUREMENTJOB</jobType>
        <jobPriority>HIGH</jobPriority>
        <granularityPeriod>FIVE_MIN</granularityPeriod>
        <jobGroup>Sla</jobGroup>
        <reportContentGeneration>CHANGED_ONLY</reportContentGeneration>
        <MeasurementReader>
            <measurementReaderId>mr_2</measurementReaderId>
            <measurementSpecification struct="MeasurementSpecification">
                <measurementTypeRef>Anything</measurementTypeRef>
            </measurementSpecification>
            <thresholdRateOfVariation>PER_SECOND</thresholdRateOfVariation>
        </MeasurementReader>
        <MeasurementReader>
            <measurementReaderId>mr_1</measurementReaderId>
            <measurementSpecification struct="MeasurementSpecification">
                <measurementTypeRef>ManagedElement=1,SystemFunctions=1,Pm=1,PmGroup=OSProcessingLogicalUnit,MeasurementType=CPULoad.Total</measurementTypeRef>
            </measurementSpecification>
            <thresholdRateOfVariation>PER_SECOND</thresholdRateOfVariation>
        </MeasurementReader>
    </PmJob>
$

This utility is installed by default on many Linux distributions.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .