bash - sed copy substring from fixed position and copy it in front of line -
i'm dealing many csv files , can't find way sed select substring @ fixed position (chars 9-16) , copy @ beginning of line.
this have:
abc09638006924340017;some_text;some_other_text
this need:
00692434;abc09638006924340017;some_text;some_other_text
the following code in sed gives substring need (00692434) overwrites whole line:
sed 's/^.{8}(.{8}).*/\1/')
i'm using sed "clean" linestrings , inserting variables, called in bash script @ end imports data in postgres. why prefer remain within sed, hint appreciated i'm not real expert.
you need escape curly braces (\{\}
) parentheses (\(\)
) , append original string (&
) in replacement:
text="abc09638006924340017;some_text;some_other_text" echo $text | sed "s/^.\{8\}\(.\{8\}\).*/\1;&/"
output:
00692434;abc09638006924340017;some_text;some_other_text
since want extract fixed-length substring @ fixed position, bash-builtins:
text="abc09638006924340017;some_text;some_other_text" echo "${text:8:8};$text"
Comments
Post a Comment