regex - Using awk or sed to merge / print lines matching a pattern (oneliner?) -
i have file contains following text:
subject:asdfghj subject:qwertym subject:bigger1 subject:sage911 subject:mothers object:cfvvmkme object:rjo4j2f2 object:e4r234dd object:uft5ed8f object:rf33dfd1
i hoping achieve following result using awk or sed (as oneliner bonus! [perl oneliner acceptable well]):
subject:asdfghj,object:cfvvmkme subject:qwertym,object:rjo4j2f2 subject:bigger1,object:e4r234dd subject:sage911,object:uft5ed8f subject:mothers,object:rf33dfd1
i'd have each line matches 'subject' , 'object' combined in order each 1 listed, separated comma. may see example of done awk, sed, or perl? (preferably oneliner if possible?)
i have tried uses of awk perform this, still learning should add:
awk '{if ($0 ~ /subject/) pat1=$1; if ($0 ~ /object/) pat2=$2} {print $0,pat2}'
but not thought would! know have syntax wrong. if see example can learn.
not perl or awk easier.
$ pr -2ts, file subject:asdfghj,object:cfvvmkme subject:qwertym,object:rjo4j2f2 subject:bigger1,object:e4r234dd subject:sage911,object:uft5ed8f subject:mothers,object:rf33dfd1
explanation
-2
2 columns
t
ignore print header (filename, date, page number, etc)
s,
use comma column separator
Comments
Post a Comment