Assuming I have a file called schema.prisma that looks like this
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = somevalue
url = env("DATABASE_URL")
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
todos Todo[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model Todo {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String @db.VarChar(255)
completed Boolean @default(false)
user User @relation(fields: [userId], references: [id])
userId Int
}
And I want to use SED to replace the second provider instance
I have crafted the following regex that gets me the line I want
\bprovider(?!.*prisma).*/
But I just can't seem to get SED to work with it
For example
sed 's/\bprovider\(?!.*prisma\)/blue/' ./prisma/schema.prisma
Should produce
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = blue
... rest of file
I know sed reads line by line so this approach to me seems like it should work.
What am I missing with this regex?
You could try matching a known pattern before the second occurrence:
Using sed
$ sed -E '/^datasource db \{/{n;s/(= ).*/\1blue/}' input_file
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = blue
url = env("DATABASE_URL")
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
todos Todo[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model Todo {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String @db.VarChar(255)
completed Boolean @default(false)
user User @relation(fields: [userId], references: [id])
userId Int
Or you could null data the entire file with the -z
flag matching the second occurrence of provider.
$ sed -Ez 's/(([^=]*provider = ){2})[^\n]*/\1blue/' input_file