Using sed / awk to process file with stanza format -


i have file in stanza format. example of file below.

id_1:         id=241         pgrp=staff         groups=staff         home=/home/id_1         shell=/usr/bin/ks id_2:         id=242         pgrp=staff         groups=staff         home=/home/id_2         shell=/usr/bin/ks 

how use sed or awk process , return id name, id , groups in single line , tab delimited format? e.g.:

id_1        241     staff id_2        242     staff 

here awk solution:

translate.awk

#!/usr/bin/awk -f {   if(match($1, /[^=]:[ ]*$/)){     id_=$1     sub(/:/,"",id_)   }   if(match($1,/id=/)){     split($1,p,"=")     id=p[2]   }   if(match($1,/groups=/)){     split($1,p,"=")     print id_," ",id," ",p[2]   } } 

execute either by:

chmod +x translated.awk ./translated.awk data.txt 

or

awk -f translated.awk data.txt 

for completeness, here comes shortened version:

#!/usr/bin/awk -f $1 ~ /[^=]:[ ]*$/ {sub(/:/,"",$1);printf $1" ";fs="="} $1 ~ /id/         {printf $2" "} $1 ~ /groups/     {print $2} 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -