awk - Replace newlines between two words -
i have output text file below. want put contents of someitems array under 1 line. so, every line have contents of new someitems array. example :
"someitems": [ { "someid": "mountsomers-showtime.com-etti0000000000000003-1452005472058", "source": "mountsomers", "sourceassetid": "9", "title": "pk_3", "ppp": "12", "expirationdate": "2016-01-06t14:51:12z" }, { "someid": "mountsomers-ericsson.com- etti0000000000000005-1452005472058", "source": "mountsomers", "sourceassetid": "12", "title": "pk_5", "ppp": "12", "expirationdate": "2016-01-06t14:51:12z" } ] "someitems": [ { "someid": "mountsomers-hbo.com-etti0000000000000002-1452005472058", "source": "mountsomers", "sourceassetid": "7", "title": "pk_2", "ppp": "12", "expirationdate": "2016-01-06t14:51:12z" }, { "someid": "mountsomers-showtime.com-etti0000000000000003-1452005472058", "source": "mountsomers", "sourceassetid": "9", "title": "pk_3", "ppp": "12", "expirationdate": "2016-01-06t14:51:12z" }, { "someid": "mountsomers-ericsson.com-etti0000000000000005-1452005472058", "source": "mountsomers", "sourceassetid": "12", "title": "pk_5", "ppp": "12", "expirationdate": "2016-01-06t14:51:12z" } ] would become
"someitems": [ ..... ] "someitems": [ ..... ] i have below
cat file | | awk '/^"someitems": [/{p=1}/^]/{p=0} {if(p)printf "%s",$0;else printf "%s%s\n",(nr==1?"":rs),$0}' but not wanted...
since input contains brackets [] in outer level solution can pretty simple:
awk '{gsub("\n","", $0)}1' rs=']\n' file i'm using ]\n input record separator. gives whole portion between "someitems: ..." until closing ] $0. gsub() replaces newlines. 1 prints (modified) record.
you can use sed:
sed '/\[/{:a;n;/]/!ba;s/\n//g}' file i'll explain in multiline version:
script.sed:
# address. matches line containing opening [ /\[/ { # start of block # define label 'a' :a # read new line , append pattern buffer n # if pattern buffer doens't contain closing ] # jump label 'a' /]/!ba # replace newlines once closing bracket appeared # since don't jump 'a' in case, means we'll # leave block , start new cycle. s/\n//g } # end of block
Comments
Post a Comment