arortell
arortell

Reputation: 23

Extracting parts of a string inside an awk script

Here is the data format I am working with.

jmontgomery:Janet:Montgomery:(312)213-9876:[email protected]
gstefani:Gwen:Stefani:(123)456-7890:[email protected]
linusT:Linus:Torvadis:(987)654-3210:[email protected]
tux:Arch:Linux:(314)921-3071:[email protected]

Here is the output I want.

dn: uid=jmontgomery [email protected] [email protected]
cn:Janet Montgomery
sn:Montgomery
telephoneNumber:(312)213-9876

dn: uid=gstefani [email protected] [email protected]
cn:Gwen Stefani
sn:Stefani
telephoneNumber:(123)456-7890

dn: uid=linusT [email protected] [email protected]
cn:Linus Torvadis
sn:Torvadis
telephoneNumber:(987)654-3210

dn: uid=tux [email protected] [email protected]
cn:Arch Linux
sn:Linux
telephoneNumber:(314)921-3071

And my script:

BEGIN { FS=":"; ORS="\n\n" }
      { print "dn: uid="$1, "email="$5, "domain="$5 "\n" "cn:"$2,$3 "\n" \
        "sn:"$3 "\n" "telephoneNumber:"$4 }
END { print "ALL DONE" }

I want to put only the domain of the email address's where "domain="$5 is. I would like to use a bash parameter expansion such as

"${$5##*@}"

But it wont let me do that.I even tried to change the FS from FS=':' to FS='@' but how could I do that for just the email address and only after it has already be processed once.I'm not against using an simple bash command such as [cut -f2 -d"@"] but how could I run that inside the awk script.I hope I explained this correctly.Thank you

Upvotes: 2

Views: 57

Answers (1)

Ed Morton
Ed Morton

Reputation: 203169

wrt I would like to use a bash parameter expansion such as - awk isn't bash or any other shell, it's a completely different/separate tool with it's own syntax, semantics, and context for variables.

$ cat tst.awk
BEGIN { FS="[:@]" }
{
    printf "dn: uid=%s email=%s@%s domain=%s\n", $1, $5, $6, $6
    printf "cn:%s %s\n", $2, $3
    printf "sn:%s\n", $3
    printf "telephoneNumber:%s\n", $4
    print ""
}

$ awk -f tst.awk file
dn: uid=jmontgomery [email protected] domain=live.com
cn:Janet Montgomery
sn:Montgomery
telephoneNumber:(312)213-9876

dn: uid=gstefani [email protected] domain=compuserv.edu
cn:Gwen Stefani
sn:Stefani
telephoneNumber:(123)456-7890

dn: uid=linusT [email protected] domain=linux.org
cn:Linus Torvadis
sn:Torvadis
telephoneNumber:(987)654-3210

dn: uid=tux [email protected] domain=archlinux.org
cn:Arch Linux
sn:Linux
telephoneNumber:(314)921-3071

If you really want to print "ALL DONE" after processing is complete then I highly recommend you print it to stderr instead of mixing it in with your normal output that's going to stdout. To do that add:

END { print "ALL DONE" | "cat>&2" }

at the end of the script.

Upvotes: 2

Related Questions