Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

awk field order (cpm)

1 view
Skip to first unread message

sandy_eggo

unread,
Jul 14, 2008, 7:52:23 PM7/14/08
to
Given a record that looks like:
Department: PEDIATRICS/TRIHEALTH

Using awk how can I print the record so it looks like:
Department: TRIHEALTH/PEDIATRICS

(basically print $2 and $1 with an OFS = "/"). I've tried several
iterations and.
just. can't. get. it. today.

Janis Papanagnou

unread,
Jul 14, 2008, 8:09:06 PM7/14/08
to

The problem is that it's not $2 and $1 that you want to swap, rather
you have a hierarchical structure, where $1 is "Department:" and $2
is "TRIHEALTH/PEDIATRICS".

Awk provides a split function that you can apply on arbitrary strings.

$1=="Department:" { split($2,arr,"/"); print $1,arr[2]"/"arr[1] }


Janis

sandy_eggo

unread,
Jul 14, 2008, 8:26:34 PM7/14/08
to
Excellent! Thank you! It's been a long weekend.

Sashi

unread,
Jul 22, 2008, 3:58:05 PM7/22/08
to

Though an acceptable solution has been posted, I'm including one more,
along the lines of what the OP sems to have tried.

awk 'BEGIN {FS="[ /]";} {print $1" "$3"/"$2;}'

This way, you can specify that a " " or a / can be used as an FS and
then print the fields desired.
Of course, you have to print the space and / too.
Sashi

0 new messages