Adam G
Adam G

Reputation: 1

replace block of text in file linux

I want to remove block of text in file.

file

      .
      .
      .
 "chromedriver": {
      "version": "80.0.2",
      .
      .
      .
   }
     },
 "ci-env": {
      .
      .
      .

I want remove attribute chromedriver witch is define in multiple lines.

I tried this:

sed -i 's/"chromedriver"(\s|\n|.)*"ci-env":/"ci-env":/g' file

but it doesn't work. There is a oneliner that can resolve my problem? Is sed can do this?

Upvotes: 0

Views: 235

Answers (2)

petrus4
petrus4

Reputation: 614

Ed to the rescue!

#!/bin/sh

cat >> edcut.txt << EOF
/chromedriver/
ka
/ci-env/
-1
kb
'a,'bd
wq
EOF

ed -s file.json < edcut.txt

if you wanted to extract the record to another file, replace 'a,'bd with 'a,'bW newfile.txt.

Upvotes: 0

Shawn
Shawn

Reputation: 52344

Using jq, which as mentioned in a comment is the better approach for dealing with JSON:

jq 'del(.chromedriver)' file > file.tmp && mv -f file.tmp file

(Guessing at the appropriate path here because your example data is way too sketchy)

Upvotes: 1

Related Questions