Xmllint: get one result per line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Xmllint: get one result per line
# 1  
Old 04-03-2017
Xmllint: get one result per line

Hi,

I'm trying to get some values from an xmlfile and want be able to process them. I'm using xmllint(v20901 on debian jessie) and this program directly outputs all results concatenated right after each other. I did not find a solution in the man page to get a different format or some output separator.

Here's the data:

XML-INPUT
Code:
<benno>
  <configuredarchives>
    <containerarchive>
      <identifier>my.archiveserver.com</identifier>
      <conditions>
        <all/>
      </conditions>
      <directorywatcher directory="/srv/benno/inboxes/my.archiveserver.com" pattern=".*.eml">
        <mailfile>
          <senderheaders>
            <name>X-REAL-MAILFROM</name>
            <name>RECIPIENT-FROM</name>
            <name>From</name>
          </senderheaders>
          <recipientheaders>
            <name>X-REAL-RCPTTO</name>
            <name>RECIPIENT-TO</name>
            <name>BCC</name>
            <name>To</name>
            <name>Cc</name>
          </recipientheaders>
          <secretheaders>
            <name>RECIPIENT-TO</name>
            <name>RECIPIENT-FROM</name>
            <name>BCC</name>
            <name>X-REAL-MAILFROM</name>
            <name>X-REAL-RCPTTO</name>
          </secretheaders>
        </mailfile>
      </directorywatcher>
      <configuredcontainers>
        <simplecontainer>
          <identifier>Foo Inc.</identifier>
          <conditions>
            <or>
              <domain sender="true" recipient="true" from="true" to="true" cc="true">foobar.de</domain>
              <domain sender="true" recipient="true" from="true" to="true" cc="true">foo.de</domain>
            </or>
          </conditions>
          <luceneindex version="LUCENE_36">
            <directory>/srv/benno/archives/my.archiveserver.com/foobar/index</directory>
          </luceneindex>
          <monthlyfsbox>
            <fshexbennobox>
              <monthlyfsjournal/>
              <directory>/srv/benno/archives/my.archiveserver.com/foobar/repo</directory>
              <subdirs>3</subdirs>
              <dirlength>2</dirlength>
              <compression>gzip</compression>
            </fshexbennobox>
          </monthlyfsbox>
        </simplecontainer>
      </configuredcontainers>
      <configuredcontainers>
        <simplecontainer>
          <identifier>Bla Blub</identifier>
          <conditions>
            <or>
              <domain sender="true" recipient="true" from="true" to="true" cc="true">blablub.de</domain>
            </or>
          </conditions>
          <luceneindex version="LUCENE_36">
            <directory>/srv/benno/archives/my.archiveserver.com/blablub/index</directory>
          </luceneindex>
          <monthlyfsbox>
            <fshexbennobox>
              <monthlyfsjournal/>
              <directory>/srv/benno/archives/my.archiveserver.com/blablub/repo</directory>
              <subdirs>3</subdirs>
              <dirlength>2</dirlength>
              <compression>gzip</compression>
            </fshexbennobox>
          </monthlyfsbox>
        </simplecontainer>
      </configuredcontainers>
    </containerarchive>
  </configuredarchives>
  <bennoRest>
    <jettyConfig>/etc/benno/jetty.xml</jettyConfig>
    <sharedSecret>verysecret</sharedSecret>
    <indexCacheTTL>300000</indexCacheTTL>
  </bennoRest>
</benno>

The used command
Code:
 xmllint --xpath "//fshexbennobox/directory/text()" /etc/benno/benno.xml

The result
Code:
/srv/benno/archives/my.archiveserver.com/foobar/repo/srv/benno/archives/my.archiveserver.com/blablub/repo

Wanted result
Code:
/srv/benno/archives/my.archiveserver.com/foobar/repo
/srv/benno/archives/my.archiveserver.com/blablub/repo

So since there is no separator-character at all, the solution is not usable.

I got a workaround, which is not that efficient, but well it works:

Code:
#!/bin/bash
data="$(cat /etc/benno/benno.xml)"
count=$(xmllint --xpath "count(//fshexbennobox/directory)" - <<<"$data")
for((i=1;$i<=$count;i++)) ; do
        xmllint --xpath "(//fshexbennobox/directory)[$i]/text()" - <<<"$data"
        echo ""
done

Maybe you have some further tip. I absolutely want to stay with xmllint.
# 2  
Old 04-03-2017
Oi. We had a similar problem -

Code:
xmllint --shell /path/to/inputfile <<< `echo 'cat /config/*/@*'`  | egrep '(pattern1|pattern2)' > outfile

The "/config" part has the commands you would manually enter.

Best I can do for you. Maybe someone else has a better solution....
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 04-03-2017
ok. Thanks. That's a bit shorter.

Code:
 xmllint --shell /etc/benno/benno.xml <<<'cat /*/*/*/*/*/*/fshexbennobox/directory/text()' \
    | grep -vE '^(/ > ?)?( +-+)?$'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Output result with break line

Hello, I am coding a script to check mysql databases using bash script, I would like to check if the status of a table is not 'OK', will return the table name and do some more actions: check.log table1 OK table2 Some error here table3 ... (5 Replies)
Discussion started by: Abu Rayane
5 Replies

2. Shell Programming and Scripting

Help with xmllint

Have like 50 xml files in a folder. They all have a Node named <Number>.How to display the values of <Number> with the count and filename in the folder. I am using Mac . (7 Replies)
Discussion started by: Anethar
7 Replies

3. Shell Programming and Scripting

How to compare the current result with previous line result.?

Hi Gurus, I have requirement to compare current result with previous reuslt. The sample case is below. 1 job1 1 1 job2 2 1 job3 3 2 job_a1 1 2 job_a2 2 2 job_a3 3 3 job_b1 1 3 job_b2 2 for above sample file, GID is group ID, for input line, the job run... (1 Reply)
Discussion started by: ken6503
1 Replies

4. Shell Programming and Scripting

Append result in the same line

I have a line like below a,blank,12,24 I want to add (12+24) at the end of the line or any where in the same line line output: a,blank,12,24,36 (2 Replies)
Discussion started by: Anjan1
2 Replies

5. Shell Programming and Scripting

Parse XML using xmllint

Hi All, Need help to parse the xml file in shell script using xmllint. Below is the sample xml file. <CARS> <AUDI> <Speed="45"/> <speed="55"/> <speed="75"/> <speed="95"/> </AUDI> <BMW> <Speed="30"/> <speed="75"/> <speed="120"/> <speed="135"/> </BMW>... (6 Replies)
Discussion started by: prasanna2166
6 Replies

6. Shell Programming and Scripting

Print the whole line which contains the result of the command cut

Hey everyone I have a file 'agenda' which contains: Object Day Month Year Birthday 09 02 2012 i want to extract from a script the line which contains the day the user typed. for example if he type 09 the line is showed using... (4 Replies)
Discussion started by: Goldstein
4 Replies

7. Shell Programming and Scripting

uniform and same result whois command line

I am looking for a free whois lookup tool or some "scripting help" that will give uniform result for whois lookup from the linux command line. Currently: whois of a .co.nz domain results nameserver as follows. ns_name_01: ns1.domain.co.nz ns_name_02: ns2.domain.co.nz While that of a .net... (5 Replies)
Discussion started by: anilcliff
5 Replies

8. Shell Programming and Scripting

What syntax is required so that result will start from a new line..

What syntax is required to start the result in a new line (7 Replies)
Discussion started by: sapan123
7 Replies

9. UNIX for Advanced & Expert Users

iconv and xmllint

Here is my question, volume of records processed : 5M ( approx ) Its basically very simple operation that am trying to do and I had achieved the output that am interested. What am looking for really is to improve the performance, an optimized way to do that. with respect to iconv, am... (3 Replies)
Discussion started by: matrixmadhan
3 Replies

10. UNIX for Dummies Questions & Answers

is there any why to get the number of line in grep result ?

Hello all when I do simple grep on file im getting the results of "filename : stringResult " is there any way to present also the line number in the file ? (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question