Pieter-Jan
Pieter-Jan

Reputation: 480

Matching pattern including newline with OS X (BSD) sed

I want to match the following patterns using sed on OSX:

test = {

and

test =
{

I tried a lot of different things, including the line below but I can't figure out why it doesn't work:

sed -n -E -e "s/(^[a-zA-Z_]*[ ]*=[ "'\'$'\n'"]*{.*)/var \1/p" $path

I used the extquote $'\n' to match newline and included a backslash in front of it as I read on many posts on the internet. If I don't use any ( or [ groups it does work but I want to use the groups. I keep getting the following error:

sed: 1: "s/(^[a-zA-Z_]*[ ]*=[ \
 ...": unbalanced brackets ([])

Can anyone help me? I'm getting quite desperate.

Upvotes: 0

Views: 243

Answers (1)

123
123

Reputation: 11216

This should work depending on what your exact data can be

sed '/[[:alpha:]]* =/{/ *{/!N;//s/^/var /;}' file

Input

test = {
blah
test =
{
wut

Output

var test = {
blah
var test =
{
wut

Upvotes: 1

Related Questions