1

This is my sample file:

<?xml version="1.0" encoding="UTF-8" ?>
 <testjar>
 <testable>
  <trigger>Trigger1</trigger>
  <message->2012-06-14T00:03.54</message>
 <sales-info>
  <san-a>no</san-a>
  <san-b>no</san-b>
  <san-c>no</san-c>
  </sales-info>
  </testable>
  </testjar>

I need to extract xml tags from this-

e.g. output of above file should be

testjar
testable
trigger
message
sales-info
....

2 Answers 2

3
$> cat ./text
<?xml version="1.0" encoding="UTF-8" ?>
 <testjar>
 <testable>
  <trigger>Trigger1</trigger>
  <message>2012-06-14T00:03.54</message>
 <sales-info>
  <san-a>no</san-a>
  <san-b>no</san-b>
  <san-c>no</san-c>
  </sales-info>
  </testable>
  </testjar>

And

$> grep -P -o "(?<=\<)[^>?/]*(?=\>)" ./text
testjar
testable
trigger
message
sales-info
san-a
san-b
san-c 

Regular expression (?<=\<)[^>?/]*(?=\>) consist of 3 parts:

  • (?<=\<): (?<=) is lookbehind operator, so it means "after <";

  • [^>?/]*: not >,?,/ 0 or more times;

  • (?=\>): (?=) is lookahead operator, so it means "before >"

Sign up to request clarification or add additional context in comments.

3 Comments

Can you explain what does '>' on the start of the command? I.e. $> cat . Пожалуйста. And why you use ./ which means an execution, afaik.
$> is my bash PS1. It indicates that following line is shell query. ./ means "current directory" ⇒ ./texttext.
I have checked if file comes in single line also, it works perfectly fine
0
awk -F">" '{print $1}' xmlfile | sed -e '/<\//d' -e '/<?/d' -e 's/<//g'

1 Comment

If file comes in single line then its not providing any output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.