Can I use the character "/" in awk

I am working with some .xml, and I have to extract part of them. I need to extract block between the line where appear >21120352</PMID> and the line where appears </PubmedArticle>

But the syntax of awk uses "/" so it gives me an error. I also have used the equivalent U+002F, but the solutions returns more than I want.

By the moment, I have this solution:

awk '/>21120352</,/PubmedArticle>/' file.xml

Also I have used the wildcard:

awk '/>21120352<.PMID>/,/<.PubmedArticle>/' file.xml

My question is if is possible to search using </PMID> and </PubmedArticle>

2 Answers

You need to escape the / with a backslash: \/.

awk '/>21120352<\/PMID>/,/<\/PubmedArticle>/' file.xml

As an alternative to awk, I suggest to have a look at XMLStarlets xml sel function which is better in parsing XML files.

0

As an alternative to escaping the pattern using a backslash \ you can also use a variable to define your pattern:

awk '$0~pattern' pattern=">21120352</,/PubmedArticle>/" filename

More workarounds.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like