| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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. |
|
#2
| |||
| |||
| 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 |
|
#3
| |||
| |||
| Excellent! Thank you! It's been a long weekend. |
|
#4
| |||
| |||
| 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 |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.