Transforming text with 'sed' or 'awk' -
i have large input set looks this:
label: foo, other text: text description... <insert label> item: item description... <insert label> item: item description... label: bar, other text:... <insert label> item:... label: baz, other text:... <insert label> item:... <insert label> item:... <insert label> item:... ...
i'd transform pull out label name (e.g. "foo"
) , replace tag "<insert label>"
on following lines actual label.
label: foo, other text: text description... foo item: item description... foo item: item description... label: bar, other text:... bar item:... label: baz, other text:... baz item:... baz item:... baz item:... ...
can done sed or awk or other unix tool? if so, how might it?
one solution using sed
:
content of script.sed
:
## when line beginning 'label' string. /^label/ { ## save content 'hold space'. h ## string after label (removing other characters) s/^[^ ]*\([^,]*\).*$/\1/ ## save in 'hold space' , original content ## of line (exchange contents). x ## print , read next line. b } ###--- commented wrong behaviour ---### #--- g #--- s/<[^>]*>\(.*\)\n\(.*\)$/\2\1/ ###--- , fixed ---### ## when line begins '<insert label>' /<insert label>/ { ## append label name line. g ## , substitute '<insert label>' string it. s/<insert label>\(.*\)\n\(.*\)$/\2\1/ }
content of infile
:
label: foo, other text: text description... <insert label> item: item description... <insert label> item: item description... label: bar, other text:... <insert label> item:... label: baz, other text:... <insert label> item:... <insert label> item:... <insert label> item:...
run like:
sed -f script.sed infile
and result:
label: foo, other text: text description... foo item: item description... foo item: item description... label: bar, other text:... bar item:... label: baz, other text:... baz item:... baz item:... baz item:...
Comments
Post a Comment