0

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.

Pieter-Jan
  • 450
  • 5
  • 9
  • Sed reads line demilted by newlines, you have to explicitly get the next line to perform a replacement on it. – 123 Jul 15 '15 at 13:19

1 Answers1

1

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
123
  • 10,778
  • 2
  • 22
  • 45
  • Thanks. I get a lot of matches with this pattern (a lot more than I want but maybe I can tweak that) but the newline still isn't matched ... maybe its impossible with this version of sed – Pieter-Jan Jul 15 '15 at 13:35
  • What do you mean the newline isn't matched ? What problem are you having, does it work on the example input i have provided, if it does can you give me some that it doesn't work on ? – 123 Jul 15 '15 at 13:38
  • @Pieter-Jan Try it now, it was because i had left a space at the end of the regex. – 123 Jul 15 '15 at 14:17
  • ok! that is perfect! you just lost my ^ at the beginning but now I get most the matches I want (except it also matches things like test = function () { which I don't want). Thank you so much! I don't really understand the command yet but I'll try ... – Pieter-Jan Jul 15 '15 at 14:29