0

I have a response from a curl command to a text file that looks like this:

<att id="owner"><val>objs/13///user</val></att><att id="por"><val>objs/8/</val></att><att id="subType"><val>null</val></att><att id="uid"><val>14</val></att><att id="webDavPartialUrl"><val>/Users/user%

I need to find the string between the string >objs/8/</val> and <att id="uid">

i have tries awk,sed and grep, but all have issues with special charterers like those above, is there an option to treat the text as simple charterers?

Ilana G
  • 53
  • 4
  • 1
    Perhaps [this question](http://stackoverflow.com/questions/15461737/how-to-execute-xpath-one-liners-from-shell) will help? – Phylogenesis Oct 11 '16 at 14:00

2 Answers2

3

Using grep with -- (explained here)

$ grep -o  -- '>objs/8/</val>.*<att id="uid">' pattern
>objs/8/</val></att><att id="subType"><val>null</val></att><att id="uid">

For more specific matching with grep, you can refer to this question.

Otherwise, because your input seems to be XML, you should consider using an XPATH expression on it. More specifically, it seems that you want to retrieve <att id="subType">, which should be easy to express. Adding <test> and </test> around your sample, I was able to use xmllint to retrieve the value.

$ xmllint --xpath '/test/att[@id="subType"]' pattern 
<att id="subType"><val>null</val></att>
Community
  • 1
  • 1
Aif
  • 11,015
  • 1
  • 30
  • 44
0

Using Perl:

perl -ne 'print "$1\n" if m#>objs/8/</val>(.*)<att id="uid">#' file

output:

</att><att id="subType"><val>null</val></att>

Explanation:

  • $1 is the captured string (.*)
  • m## is used here as the matching operator instead of the standard Perl //, in order to ignore the special / characters
Chris Koknat
  • 3,305
  • 2
  • 29
  • 30