1

There are many lines in a text file and I would like to change the value true to false using a one line script.

bash-3.2$ grep online test.txt
# online = false --> ONE SHOT import (delete all table contents before importing)
# online = true  --> DIFFERENTIAL import (import without clean the table contents)
online =  true

I tried this

sed -i.bak '/DIFFERENTIAL\|/ONE SHOT\/!s/online.*/online = false/g'

but it gave this error:

sed: -e expression #1, char 17: unknown command: `O'

I don't want the first 2 lines to change, only the third online = true to become online = false. It can be anywhere in the file but it will match online =. So I would want my command to ignore 1 and 2 and change 3 only.

2
  • \|/ONE SHOT: looks like you have an extra slash in there. Commented Mar 31, 2015 at 17:05
  • 1
    sed -i.bak '/DIFFERENTIAL\|ONE SHOT/!s/online.*/online = false/g' test Commented Mar 31, 2015 at 17:08

2 Answers 2

2

If you are only looking to alter lines starting with online, it's perhaps easiest to do this:

sed 's/^online\ *=.*/online = false/g'

This way, the lines starting with # are completely ignored.

An even simpler (but not identical) alternative, suggested by @mikeserv:

sed '/^online/s/true/false/'
2
  • @mikeserv: Yeah. I don't know for sure how stringent the question's requirement to match online = is... I'll integrate your suggestion to my answer; the asker can choose what they prefer. Commented Mar 31, 2015 at 19:28
  • I take your point - and it's well made. Commented Mar 31, 2015 at 20:46
1

Via :

awk '/online =/{c++} c==3{gsub("true","false")}1' test.txt

This will match all occurrences of online = and increment the counter flag c++; When 3 occurrences found c==3, then replace the true with false.

1
  • The OP stated that the pattern can occur anywhere in the file. Commented Mar 31, 2015 at 17:42

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.