-2
    #!/usr/bin/awk -f
    
    BEGIN {
            FS="><";
            print "XML Tags";
    }
    {
            for(x=1; x<=NF; x++) {
                    if (x==1) {
                            f=$x">";
                    } else {
                            f="<"$x">";
                    }
                    if (f!="\n") {
                            printf f"\n";
                    }
            }
    }
    END {
            print "End of tags";
    } $1;

Hi all,

I have an XML file that is all on one line. I am using the above AWK script to break it into lines. The script produces each field on a separate line, and then it prints the whole line again.

As this is a learning exercise for me would somebody be able to point out where I have gone wrong?

When I add a pattern as the condition in front of the default action I still get the same output, which is as explained above. The pattern I added was /SIZE/, which is a word in the only line in the file.

The output I am seeing is the same on my Gentoo box and my AIX box. So it must be my code.

It is driving me nuts ...

HonkeyPig
  • 1
  • 2
  • 1
    Don't use the word "pattern" as it's ambiguous (see https://stackoverflow.com/q/65621325/1745001). Also usually when people say "pattern" in reference to an awk script they actually mean "condition" so it's really unclear what "When I add a pattern in front of the condition" might mean. Please add concise, testable sample input and expected output that demonstrates your problem. – Ed Morton May 23 '21 at 21:31
  • Please add sample input (no descriptions, no images, no links) and your desired output for that sample input to your question (no comment). – Cyrus May 23 '21 at 21:32
  • 2
    I suggest to remove `$1` from your script. – Cyrus May 23 '21 at 21:43
  • And don't use a shebang to call awk, see https://stackoverflow.com/a/61002754/1745001. – Ed Morton May 23 '21 at 21:52
  • And don't do `printf f"\n"`, do `print f` or `printf "%s\n", f` instead. `printf f"\n"` will fail if/when `f` contains `printf` formatting characters like `%s`. That applies to any use of `printf` for input data. Finally - `f` can never be `\n` (nor can it even contain `\n`) so your comparison `if (f!="\n")` will always be true. – Ed Morton May 23 '21 at 22:29
  • I'm just running through the comments, and have updated the question as per Ed's first comment. I did mean to put action not condition, but it was in fact true that it was not clear. The no shebang is also a very good shout and will be incorporating that from now on. I'm just starting out with AWK, so i had no consideration of this script being anything other than stand-alone, but also I had not considered its limitations yet. So thank you for that. Going to make coffee and get to that last comment which I anticipate to be helpful also considering the current 2 out of 2! xD – HonkeyPig May 23 '21 at 23:15
  • Oh, and if the last comment does not help provide the solution I will knock up some test data. It has so far given me the same results for an XML file where it is concatenated to the same line – HonkeyPig May 23 '21 at 23:16
  • So helpful again, good point about the characters in the file conflicting with printf and the silly condition for print in the first place. – HonkeyPig May 23 '21 at 23:41
  • Removing the $1 from the bottom of the script has solved the issue. Thank you very much Cyrus. From something I read along the way I must have gotten the wrong idea about the file I was appending to my script call - ./xml.awk someOneLineXMLFile.xml – HonkeyPig May 23 '21 at 23:46

2 Answers2

0

Instead of fight with awk you can use xmllint to make pretty print of XML:

xmllint --format filename

For Solaris you can check this man page

Also you can check this Q/A

Romeo Ninov
  • 6,538
  • 1
  • 22
  • 31
  • Thank you for the suggestion Romeo, I have it on my Gentoo box but I'm not sure about the Solaris AIX box - it is pretty outdated and in production - which means it has been stripped down. I'll defo have a look though for future endeavors, however, this is purely just learning for AWK. – HonkeyPig May 25 '21 at 11:14
  • @HonkeyPig, seems xmllint is standard part of Solaris (at least 11). About AIX not sure. Check my edited answer – Romeo Ninov May 25 '21 at 11:31
  • 1
    Cheers Romeo :) I'm going to check on the server soon in work time and see if it is there - they could still have removed it. – HonkeyPig May 25 '21 at 12:50
  • xmllint is not there – HonkeyPig May 25 '21 at 21:11
-1

The answer is remove the $1 from the end of the script - it is a left over from running AWK from within a ksh script

HonkeyPig
  • 1
  • 2