0

This cmd works fine on terminal line on RHEL7

grep -oPm1 '(?<=<app my_id=")[^<]+"my_name=
"xyz"+[^>] test.xml

But if I insert the line into a .sh script like:

grep -oPm1 '(?<=<app my_id=")[^<]+"my_name=
'"$var"'+[^>] test.xml

Where
$var="xyz"

It returns as blank.

I will hopefully have the above cmd in a loop where var is read from a text file.

user1406476
  • 69
  • 1
  • 7
  • 1
    It's quite confusing about your characters. Please edit it, with all half-width characters. Possibly you typed those with some kind of IME. – Til Jan 04 '19 at 15:34
  • 4
    I have a hard time believing that your first command works fine, it uses "smart" quotes (`‘”`) instead of the "normal" ones (`'"`), and even if it didn't, there is no closing single quote. – Benjamin W. Jan 04 '19 at 15:39
  • @BenjaminW. I have updated original question; can you help please? – user1406476 Jan 04 '19 at 17:39
  • I think it could be to do with the xml format; but any escaping I've tried in the script hasn't worked. – user1406476 Jan 04 '19 at 17:41
  • 2
    On [how to parse XML with regex](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags). Have you considered using an XML tool instead? – that other guy Jan 04 '19 at 17:45
  • 2
    The command you say "works fine" still has no closing single quote, so it's probably not the command you're actually using. And there is a line break in the pattern, which grep can't handle with the options you're using. – Benjamin W. Jan 04 '19 at 17:45
  • Where’s the ‘ missing? – user1406476 Jan 04 '19 at 17:53
  • You also need to provide a sample of the XML you are trying to process and the result you are trying to achieve. Also, you'd better use an XML tool instead as @that other guy said. I would try `xmlstarlet`. – wef Jan 07 '19 at 05:44

0 Answers0