How to mach a string when not followed by another with sed -


does knows how match sed each 'foo' instance excepted when following 'bar' in following string?

'foo       boo      foo    foo       bar foo     foo         foo'

desired result (matched instances in bold)

'matched       boo      matched    foo       bar matched     matched          matched'

after big amount of tests, found:

sed -e "s/foo\( *\)bar/foo\1bar/g" -e "s/foo/matched/g" -e "s/foo\( *\)bar/foo\1bar/g" 

it consists in first matching 'foo bar' instances , replacing them temporary string (here 'foo bar'), in matching resting 'foo' instances before replacing "back" 'foo bar' ones original version... (i hope clear...)

but anyway not clean @ all. surprised there not more straight way it, if have not been able find out far.

any hint appreciated. :-)

thank much,

if have stick sed, idea ok. if original string has foo bar, solution fails. have choose right temp string. makes script insecure.

you improve not choosing regular string temp string, invisible strings.

for example, line works:

sed -r 's/foo( *)bar/\x94\1\x98/g; s/foo/matched/g;s/\x94( *)\x98/foo\1bar/g' file 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -