sed command to replace a value in config file

24,237

Solution 1

Use sed:

sed -i 's/\(^Key2=\).*/\1Value4/' file

The -i flag activates seds in-place editing. It searches for a line that begins with Key2= and replaces the line with the key (\1) followed by the new value Value4.

Edit:

If you have slashes or other command characters used in sed, you have to escape them:

sed -i 's/\(^Key2=\).*/\1long\/value\'/with\$many\"strange\^characters/' /path/to/file

Edit 2: In your case:

sed -i  's/\(^TestT_STOA_TS1901=\).*/\1N/' /app/test/must/untuio.cf

Solution 2

Couple of ways :

  1. Probably the most straight forward way, matching the appropriate key by /Key2/ and then replacing Value2 with Value4 :

    $ sed '/Key2/ s/Value2/Value4/' foo.txt 
    Key1=Value1            
    Key2=Value4          
    Key3=Value3
    
  2. If you know that the digits will come at last and if you just want to replace those digit(s) with something else (here replacing 2 with 4) :

    $ sed '/Key2/ s/[[:digit:]]\+$/4/' foo.txt 
    Key1=Value1
    Key2=Value4
    Key3=Value3
    

I also have some other ways on top of my head but they depends on your use case. Let us know if these would suffice.

Your mistakes in sed -i -e '/KEY=/ s/= .*/= new_value/' /path/to/file :

  • As per your example, no Key will be matched with /KEY=/ as it is not in proper cases and there is a number after Key and = is after that

  • s/= .*/= new_value/, from your example there is no space after = but your pattern-substitution seems it has a space in the actual file. Otherwise as far as your given example is concerned there is no space after =.

Solution 3

Using awk? =)

awk -F= '$1=="Key2" {printf "%s=%s\n",$1,"Value4"; next}1' your_input_file

Example

The input file foo

cat foo

Key1=Value1            
Key2=Value2          
Key3=Value3

The command output

% awk -F= ' $1=="Key2" {printf "%s=%s\n",$1,"Value4"; next}1' foo
Key1=Value1            
Key2=Value4
Key3=Value3
Share:
24,237

Related videos on Youtube

canonical
Author by

canonical

Updated on September 18, 2022

Comments

  • canonical
    canonical over 1 year

    I am using this command:

    sed -i  's/\(^TestT_STOA_TS1901=\).*/\1N/'/app/test/must/untuio.cf
    

    And I am getting this error:

    XXXXXXXXX:/app/test/must>sed -i  's/\(^TestT_STOA_TS1901=\).*/\1N/'/app/test/must/untuio.cf
    sed: -e expression #1, char 33: unknown option to `s'
    

    Why is this happening? How can I replace a value in a config file?

  • canonical
    canonical over 8 years
    sed -i 's/(^Key2=).*/\1Value4/' file...this didnt work
  • chaos
    chaos over 8 years
    @canonical Use the exact command as in my answer. I used backslashes before the brackets.
  • canonical
    canonical over 8 years
    i get this error : sed: -e expression #1, char 33: unknown option to `s'
  • chaos
    chaos over 8 years
    @canonical Has the string you want to replace slashes / in it? (Value4 in the example). If yes, you need to escape them: sed -i 's/\(^Key2=\).*/\1value\/to\/replace/' file. Or post the exact command you entered.
  • canonical
    canonical over 8 years
    this is the exact command i used sed -i 's/(^TestT_STOA_TS1901=).*/\1N/'/app/test/must/untui‌​o.cf I am somehow missing the blackslash when pasting here.But i am actually using it
  • chaos
    chaos over 8 years
    @canonical See the edit of my answer. Else we need the exact command and error. Post it in your question or between backticks or see askubuntu.com/editing-help
  • fedorqui
    fedorqui over 8 years
    Note you can also say awk 'BEGIN{FS=OFS="="} $1=="Key2" {$2="Value4"}1' file so you don't have to "next".
  • canonical
    canonical over 8 years
    I edited the question now
  • chaos
    chaos over 8 years
    @canonical Use this: sed -i 's/\(^TestT_STOA_TS1901=\).*/\1N/' /app/test/must/untuio.cf. After between the sed command and the file must be a space, else it is interpreted as command, and therefore spits an error.
  • A.B.
    A.B. over 8 years
    @canonical If you like my answer, just click the little grey on the left hand site of my answer now turning it into beautiful green. If you do not like my answer, click on the little grey down-arrow below the 0, and if you really like my answer, click on the little grey checkmark and the little up-arrow... If you have any further questions, go to askubuntu.com/questions/ask