weblog d’un abbe

20060311

an AWKward day

Filed under: Experiments — abbe @ 1754

A day towards learning AWK. Although I’ve not planned my day to learn AWK, but it happened accidentally (or by chance ;-)). Today, I was in my practical class, and there I saw an old machine is running Fedora Core 1. I thought what I can do with that system, since the system doesn’t has any development tools. It has tools without documentation. Then I thought AWK might be there so, why not learn AWK. Because, I’ve tried learning AWK previously too many times, but wasn’t successful (it was not tough, but because I don’t know what to do with that language). So today, I thought why not XMLify, the /etc/passwd (one of the primary target of awk tutorials) and /etc/group. And then, I opened its infopages by executing info awk. but oops info is displaying its manpages. Then I recalled that on GNU/Linux machines AWK comes GAWK, so I did info gawk. And started reading Getting started.

So, within half-an hour I’ve produced my AWK script to XMLify /etc/passwd and /etc/group. Here I’m giving my AWK script to /etc/passwd.

# passwd2xml.awk: An AWK script to transform /etc/passwd file into passwd.xml
BEGIN { 
 FS=":";
 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<passwd>";
}
/:/  {
 printf "\t<user id=\"%s\" uid=\"%s\" gid=\"%s\" home=\"%s\" comment=\"%s\" password=\"%s\" shell=\"%s\"/>\n", $1, $3, $4, $6, $5, $2, $7;
}
END {
 print "</passwd>";
}

You can execute this script and then pipe that output to xmllint to check for well-formedness of the document as shown below:

[wahjava@pc awk]$ awk -f passwd2xml.awk /etc/passwd |xmllint -

GAWK also comes for Windows and available here.

BTW, this script is not the correct way to XMLify the /etc/passwd since XMLifying needs entitifying some characters e.g. <, > etc.

Advertisements

1 Comment »

  1. Nice blog & website…
    keep it up :)

    Comment by makuchaku — 20060315 @ 1303


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: