0

The file is like this

more /etc/sysconfig/chronyd
# Command-line options for chronyd
OPTIONS=""

We need to set the value -4 inside ""

The file looks like the following,

# Command-line options for chronyd
OPTIONS="-4" 

We can use sed as

sed -i s'/OPTIONS=""/OPTIONS="-4"/g' /etc/sysconfig/chronyd

But this not so good option because (if there is a space inside double quotes)

For example,

more /etc/sysconfig/chronyd
# Command-line options for chronyd
OPTIONS=" "
  • There is a typo in your sed call: the s command should be inside the single quotes (although I'm sure this was not the problem). – AdminBee Oct 22 at 13:14
1

Use sed addressing on RE:

sed -e '/^OPTIONS=/s/.*/OPTIONS="-4"/' /etc/sysconfig/chronyd

Mean:

  • On lines matching ^OPTIONS= (begining by OPTION=),
    • do replace everything by OPTIONS="-4".
2

The following should work,

  • If space is optional,
sed -r 's/OPTIONS="[[:space:]]?"/OPTIONS="-4"/g'
  • If space is at least one or more,
sed -r 's/OPTIONS="[[:space:]]+"/OPTIONS="-4"/g'

Or both possibilities [As mentioned by @AdminBee],

  • If space is at least one or more or optional,
sed -r 's/OPTIONS="[[:space:]]*"/OPTIONS="-4"/g'

For example,

[arif@arif]$ printf '# Command-line options for chronyd\nOPTIONS=" "\n'
# Command-line options for chronyd
OPTIONS=" "
[arif@arif]$ printf '# Command-line options for chronyd\nOPTIONS=" "\n' | sed -r 's/OPTIONS="[[:space:]]"/OPTIONS="-4"/g'
# Command-line options for chronyd
OPTIONS="-4"
  • You could also merge both possibilities by stating it as [[:space:]]* ... – AdminBee Oct 22 at 13:16
  • sed -r 's/OPTIONS="[[:space:]]+"/OPTIONS="-4"/g' /etc/sysconfig/chronyd not works when no space between the double quota – yael Oct 22 at 13:19
  • This is right. As muhammad mentioned, the syntax with [[:space:]]+ applies if space is at least one or more. If it is optional, you have to use either [[:space:]]? or [[:space:]]*. – AdminBee Oct 22 at 13:28
1

Try this,

sed  's/^OPTIONS=.*/OPTIONS="-4"/g' /etc/sysconfig/chronyd

or

sed  's/\(^OPTIONS="\).*"/\1-4"/g' /etc/sysconfig/chronyd
  • what in case space between new line to OPTIONS ? – yael Oct 22 at 13:20
  • @yael can u plz update the same in question.. – msp9011 Oct 22 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.