I want to convert every occurrence of a ';' into '@', but only if it is not quoted (discard the quotes in this line). The reason behind it:
Assembly language syntax of arm-asm has ';' as the comment sign and everything after a ';' is a comment. Despite of something like ';' or ";".
I suggest the following 'sed' script as a solution and would like to put it to discussion, in case I overlooked something:
File s:
s/^\([^(;\'\")]*\)\(;\)\(.*$\)/\1@\3/
File testcase:
;
;
;**********;****
';'
";"
';'
";"
ABC r1,';'
ABC r1,";"
; ";" ';'
;;;
Usage:
$ sed -f s testcase
@
@
@**********;****
';'
";"
';'
";"
ABC r1,';'
ABC r1,";"
@ ";" ';'
@;;
I found that e.g.
.include "stm32f407.s" ; target register defines
.set PLLP, (DIVP - 2) / 2 ; some other comment
doesn't pass the test.
So hopefully this makes it now:
#!/bin/sh
sed -e "s/^\([^(;\'\")]*\)\(;\)\(.*$\)/\1@\3/" -e "s/^\([^;]*\)\(;\)\(.*\)/\1@\3/" $1
Caveat: the script works for BSD sed (e.g., macOS), not GNU sed.