The presence of an additional character error with sed when using curly braces or loops

I am currently writing a script package to read and modify some configuration files on Windows. Fortunately, I have access to the above, and so I can use this for most of what I need to do, albeit with some quirks because of the environment.

The statement I would like to execute is as follows:

sed '/\[$/{:loop N /^^]/!b loop} s/\n//g' 

Which should theoretically remove any newlines on the lines between lines with square brackets. I use double patterns to exit and use the second (quirk cmd.exe ). However, it does not work with the following error:

 sed: -e expression #1, char 15: extra characters after command 

When testing some other statements, I got the following results.

sed '/\[/{ s/.*//g }' - Runs exactly as it should

sed '/\[/{ N } s/.*//g' - Crash with sed: -e expression #1, char 11: extra characters after command

sed 's/^^ */^&\n/ :loop s/.*/asdf/ b loop' - Failed with sed: -e expression #1, char 12: unknown option to s'``

I'm just not sure if my error is related to my main expression due to the fact that I'm twisting the syntax, or if it's just because I'm on Windows. Any thoughts or help you can provide would be great.

+6
windows unix regex cmd sed
source share
1 answer

sed '/\[$/{:loop; N;/]$/!b loop}; s/\n//g'

You need to put half-columns between your statements. In addition, I used the anchor β€œ$” instead of β€œ^” because you will never match β€œ/ ^] / ', because yourβ€œ N ”command will change the pattern space to have material up toβ€œ [”. Remember that '//' matches the template space, not the current input line.

Test

 $ echo -e 'A\n[\nfoo\nbar\nbaz\n]\nB' | sed '/\[$/{:loop; N;/]$/!b loop}; s/\n//g' A [foobarbaz] B 
+7
source share

All Articles