awk field order (cpm)

This is a discussion on awk field order (cpm) within the awk forums in Programming Languages category; 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....

Go Back   Application Development Forum > Programming Languages > awk

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 07-14-2008, 07:52 PM
sandy_eggo
Guest
 
Default awk field order (cpm)

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.
Reply With Quote
  #2  
Old 07-14-2008, 08:09 PM
Janis Papanagnou
Guest
 
Default Re: awk field order (cpm)

sandy_eggo wrote:
> 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.


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
Reply With Quote
  #3  
Old 07-14-2008, 08:26 PM
sandy_eggo
Guest
 
Default Re: awk field order (cpm)

Excellent! Thank you! It's been a long weekend.
Reply With Quote
  #4  
Old 07-22-2008, 03:58 PM
Sashi
Guest
 
Default Re: awk field order (cpm)

On Jul 14, 7:52 pm, sandy_eggo <cpmad...@gmail.com> wrote:
> 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.


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
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 08:45 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.