regex - Sed substitution for text -


i have kind of text:

0 4.496 4.496 plain text. 7.186 plain text 10.949 plain text 12.988 plain text 16.11 plain text 17.569 plain text esp: plain text 

i trying make sed substitution because need aligned after numbers, like:

0 4.496 4.496 plain text. 7.186 plain text 10.949 plain text 12.988 plain text 16.11 plain text 17.569 plain text esp:plain text 

but i'm trying different combinations of commands sed can't keep part of matching pattern

sed -r 's/\([0-9]+\.[0-9]*\)\s*/\1/g' 

i trying remove \n after numbers , align text, not work. need align text text too.

i tried well:

sed -r 's/\n*//g' 

but without results.

thank you

this bit tricky. approach not work because sed operates in line-based manner (it reads line, runs code, reads line, runs code, , forth), doesn't see newlines unless special things. we'll have override sed's normal control flow.

with gnu sed:

sed ':a $!{ n; /\n[0-9]/ { p; s/.*\n//; }; s/\n/ /; ba }' filename 

this works follows:

:a                # jump label looping (we'll build our own loop,                   # black jack and...nevermind) $! {              # unless end of input reached:   n               # fetch line, append have   /\n[0-9]/ {     # if new line begins number (if pattern space                   # contains newline followed number)     p             # print newline     s/.*\n//      # remove before newline   }   s/\n/ /         # remove newline if still there   ba              # go (loop) }                   # after end of input drop off here , implicitly                   # print last line. 

the code can adapted work bsd sed (as found on *bsd , mac os x), bsd sed bit picky labels , jump instructions. believe

sed -e ':a' -e '$!{ n; /\n[0-9]/ { p; s/.*\n//; }; s/\n/ /; ba' -e '}' filename 

should work.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -