Tuesday, 15 January 2013

linux - get a variable inside and/or outside a tuple using sed or perl -



linux - get a variable inside and/or outside a tuple using sed or perl -

input file:

{custom:{ver:120,name:hello,form:123},name:world,browser:ie} {custom:{ver:130,name:test,form:123},browser:ie}

sed command:

sed 's/^.*name:\([^,]*\).*$/\1/' input.txt

output :

world test

how differentiate variable within tuple , outside tuple?

expected output value of name within custom:

sed command within custom name

hello test

sed command outside custom name:

world blank or {custom:{ver:130,name:test,form:123},browser:ie}

note: name can appear anywhere in file--beginning, middle or end--or can absent.

perl work me. give thanks you.

name within custom using rather unsophisticated regex:

perl -lne 'print /custom:\{[^{}]*name:([^,}]*)/ ? $1 : ""' input.txt

outputs:

hello test

name outside custom:

perl -lne 'print /custom:\{[^{}]*\}(*skip)(*fail)|name:([^,}]*)/ ? $1 : ""' input.txt

outputs:

world

switches:

-l: enable line ending processing -n: creates while(<>){...} loop each “line” in input file. -e: tells perl execute code on command line.

linux perl shell sed

No comments:

Post a Comment